eval - R evalute formulas in a list of formulas -


i have list of formulas this:

ati<-list(quote(-(1/(c1 * rf) + (1/(c1 * rc) + (1/(c1 * rint) + 1/(c1 * rei))))),        quote(1/(c1 * rei)), 0, quote(1/(c1 * rint)), quote(1/(c1 * rf)),       quote(1/(c1 * rc)), 0, 0)  list of 8 $ : language -(1/(c1 * rf) + (1/(c1 * rc) + (1/(c1 * rint) + 1/(c1 * rei)))) $ : language 1/(c1 * rei) $ : num 0 $ : language 1/(c1 * rint) $ : language 1/(c1 * rf) $ : language 1/(c1 * rc) $ : num 0 $ : num 0 

i want evaluate each formula after insert value of each parameter have in other list. output want list of result. try different approach did found solution. have several of , i'd smart solution automize process.

first insert value of parameters. ati list name , value list value , name of parameters stored.

   value<-c( 0.110546010137993, 9.95953827321208, 7.1751380374394, 285.658095052437,    285.967649817749)      names(value)<-c( "c1", "rc", "rei", "rf", "rint")  ati<-gsub("c1",value[["c1"]],ati) ati<-gsub("rf",value[["rf"]],ati) ati<-gsub("rc",value[["rc"]],ati) ati<-gsub("rint",value[["rint"]],ati) ati<-gsub("rei",value[["rei"]],ati) 

the result looks have list of characters , need expression evaluation. use parse.

ati<-parse(text=ati) ati<-eval(ati) 

the result of 0 not correct. expected list of 8 number 3 0. suppose error on parse because result of apply parse is:

structure(expression(-(1/(0.110546010137993 * 285.658095052437) +  (1/(0.110546010137993 * 9.95953827321208) + (1/(0.110546010137993 * 285.967649817749) + 1/(0.110546010137993 * 7.1751380374394)))),  1/(0.110546010137993 * 7.1751380374394), 0, 1/(0.110546010137993 * 285.967649817749),  1/(0.110546010137993 * 285.658095052437), 1/(0.110546010137993 * 9.95953827321208),  0, 0),  srcfile = <environment>, wholesrcref = structure(c(1l, 0l, 9l, 0l, 0l, 0l, 1l, 9l), srcfile = <environment>, class = "srcref")) 

and seems have expression, not 8, have 1 result.

i try evaluate each formula alone, in case have different error. first formula don't have problem using code above. second 1 parse doesn't work because doesn't "/".

the second formula is:

a<-quote(1/(c1 * rei)) a<-gsub("c1",value[["c1"]],a) a<-gsub("rei",value[["rei"]],a) 

after using gsub looks wired

c("/", "1", "(0.110546010137993 * 7.1751380374394)") 

and reason, probably, when apply parse..

a<-parse(text=a) error in base::parse(...) : <text>:1:1: unexpected '/' 1: /    ^ 

any idea appreciated. thanks

gsub character strings, not language objects. (and note formula class of object in r (not have here)

if want calculate values (results) given set of parameters use eval.

if want substitute in values , return language objects, take do.call, substitute, trickery

# smaller example  l <- list(quote(-(1/(c1 * rf) + (1/(c1 * rc) + (1/(c1 * ree) + 1/(c1 * rei))))) ,quote(1/(c1 * rei)), 0)  ati <- list(c1  = 0.11054601,             ra  =   0.04522716,             rc  =   9.95953827,             ree =   1.65135221,             rei =   7.17513804,             rf  = 285.65809505)    # find result use eval  sapply(l, eval, env= ati) # [1] -7.678626  1.260743  0.000000  # substitute values symbols  lapply(l, function(x) eval(do.call('substitute',list(enquote(x), env = ati)))) str(res) # list of 3 # $ : language -(1/(0.11054601 * 285.65809505) + (1/(0.11054601 *      9.95953827) + (1/(0.11054601 * 1.65135221) + 1/(0.11054601 *  ... # $ : language 1/(0.11054601 * 7.17513804) # $ : num 0 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -