Re: [R] Reference variables by string in for loop

From: Nick Sabbe <nick.sabbe_at_ugent.be>
Date: Fri, 29 Apr 2011 13:04:57 +0200

Hi Michael.
This is a classic :-)

ObjectsOfInterest<- list(one_df, two_df, three_df) for(namedf in ObjectsOfInterest){...}

or probably even better
sapply(ObjectsOfInterest, function(namedf){...})

hth.

Nick Sabbe

--
ping: nick.sabbe_at_ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36

-- Do Not Disapprove




-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On
Behalf Of Michael Bach
Sent: vrijdag 29 april 2011 12:03
To: r-help_at_r-project.org
Subject: [R] Reference variables by string in for loop

Dear R Users,

I am trying to get the following to work better:

namevec <- c("one", "two", "three")
for (name in namevec) {
    namedf <- eval(parse(text=paste(name, "_df", sep="")))

...
...
} The rationale behind it being that I created variables with names one_df, two_df and three_df earlier in the same script which I want to reference inside the for loop. Is there a more elegant way to do this? Best Regards, Michael Bach ______________________________________________ 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. ______________________________________________ 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.
Received on Fri 29 Apr 2011 - 11:09:25 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 29 Apr 2011 - 12:20:53 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive