r - Using strings saved in variables in coding -


i know how use stings stored in variable/vector/matrix etc

in middle of coding in order edit names of variables/vectors or functions

e.g.

instead of creating below

v_name1<- rep(5,10) v_name2<- rep(30,10) . . .    function_name1<- { ... } function_name2<-{ ... }   . . .  

using loop instead

i know e.g. (this example illustrate)

  s<-c("name1","name2", ... )  (i in 1:(length(s))) {     eval(parse(text=paste("v_",s[1],"<-c(rep(i,10))", sep="")))     }         v_name1          [1] 1 1 1 1 1 1 1 1 1 1 

i have 2 problems :

1) how make functions, below error? e.g.

st<- "(x,y)<-{ 3*x + y}" eval(parse(text=paste("function_",s[1],st, sep="")))  > eval(parse(text=paste("function_",s[1],st, sep=""))) error in eval(expr, envir, enclos) : object 'x' not found 

2) there faster/smarter way (substitute part of coding strings saved object) ?

i not want share end goal or why need this, want know if 2 things above plausible.

the first comment told use lists. can put list, data functions:

set.seed(42) v <- list(uk = rnorm(10, mean = 5), = rnorm(10, mean = 30), fr = rnorm(10, mean = 300)) funs <- list(uk = mean, = median, fr = min) 

you can this:

country <- "us" funs[[country]](v[[country]]) 

r not language macros. work language , not against design. , forget eval(parse()).


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -