I would like to know how to use stings stored in a variable/vector/matrix etc
in the middle of coding in order to edit names of variables/vectors or functions
e.g.
instead of creating the below
v_name1<- rep(5,10) v_name2<- rep(30,10) . . . function_name1<- { ... } function_name2<-{ ... } . . . using a for loop instead
I already know e.g. (this is just an example to illustrate)
s<-c("name1","name2", ... ) for (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 two problems :
1) How do you make this for functions, I get the 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) is there any faster/smarter way to do just that (substitute part of the coding with strings saved into another object) ?
I do not want to share what is my end goal or why I need to do this, I just want to know if the 2 things above are plausible.
dataList <- split(data, data$country), why function names have to be "country" specific?dataobject withCountrycolumn, at the beginning set a variablecountyCode <- "UK", then work with subsetted datadataSubset <- data[ data$Country == countryCode, ], and the rest of your codes and functions should stay the same, working ondataSubsetobject only.