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

UriB uriblass at gmail.com
Wed Jul 13 13:03:17 CEST 2011


Greg Snow-2 wrote:
"You are suffering from the fact that the longest distance between 2 points
is a shortcut. 

The df$column notation is a shortcut for df[[column]] that has some nice
properties, but the shortcut gets in the way when you want to do something
more structured.  Try qq1[[z]]==y and avoid all that pasting, parsing, and
evaluating. "


My response:
Unfortunately I find it to be not correct and unfortunately I see no way to
avoid pasting parsing and evaluating in order
to have functions that does simple things in R. 

temp<-qq1[qq1$PrimaryConditionGroup=="AMI",]
has no error when 
temp<-qq1[qq1[[PrimaryConditionGroup]]=="AMI",] cause the following error
announcement

"Error in (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else
.subset2(x,  : 
  object 'PrimaryConditionGroup' not found"

I found no way to replace the function that was in the subject of this
thread by something simpler(and here is the function again)

> 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-tp3645594p3664747.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list