[R] How to translate string to variable inside a command in an easy way in R

UriB uriblass at gmail.com
Tue Jul 5 12:29:41 CEST 2011


I want to write a function that get 2 strings y and z and does the following
R command.

temp<-qq1[qq1$z==y,]
for example if it get y="AMI" and z="PrimaryConditionGroup"
It should do the following
temp<-qq1[qq1$PrimaryConditionGroup=="AMI",]

I could do it by the following function that is ugly and I wonder if there
is an easier way to do it espacielly when temp is not the final result that
I want (so I practically do not have temp<<-temp because I do not need the
function to remember temp but only to remember something else that is
calculated based on temp).

ugly<-function(y,z)
{
  text1<-paste("temp<-qq1[qq1$",z,sep="")
  text1<-paste(text1,"==y",sep="")
  text1<-paste(text1,",]",sep="")
  eval(parse(text=text1))
  temp<<-temp
}


 

--
View this message in context: http://r.789695.n4.nabble.com/How-to-translate-string-to-variable-inside-a-command-in-an-easy-way-in-R-tp3645594p3645594.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list