[R] How to translate string to variable inside a command in an easy way in R
Greg Snow
Greg.Snow at imail.org
Tue Jul 5 20:50:46 CEST 2011
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.
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of UriB
> Sent: Tuesday, July 05, 2011 4:30 AM
> To: r-help at r-project.org
> Subject: [R] How to translate string to variable inside a command in an
> easy way in R
>
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list