Seth
2010-Jun-26 00:10 UTC
[R] use a data frame whose name is stored as a string variable?
Hi, Let's say I have a data frame (called "example") with numeric values stored (columns V1 and V2). I also have a string variable storing this name x1<-"example" Is there a way to use the variable x so that R knows that I want the specified action to occur on the data frame? For example, summary (x) would return a summary of the data frame? I am considering this because I need to compare many data frames within 2 nested for loops. In the first iteration of the loop I could concatenate x and 1 and then use it to represent the data frame. I'm open to a better solution. Thanks, Seth Myers -- View this message in context: http://r.789695.n4.nabble.com/use-a-data-frame-whose-name-is-stored-as-a-string-variable-tp2269095p2269095.html Sent from the R help mailing list archive at Nabble.com.
Joshua Wiley
2010-Jun-26 00:18 UTC
[R] use a data frame whose name is stored as a string variable?
On Fri, Jun 25, 2010 at 5:10 PM, Seth <sjmyers at syr.edu> wrote:> > Hi, > Let's say I have a data frame (called "example") with numeric values stored > (columns V1 and V2). ?I also have a string variable storing this name > > x1<-"example" > > Is there a way to use the variable x so that R knows that I want the > specified action to occur on the data frame? ?For example, summary (x) would > return a summary of the data frame??get For example: get(x) # one object mget(x, envir=.GlobalEnv) # for multiple objects ## just change the environment if that is not where they are located> > I am considering this because I need to compare many data frames within 2 > nested for loops. ?In the first iteration of the loop I could concatenate x > and 1 and then use it to represent the data frame. ?I'm open to a better > solution. ?Thanks, Seth MyersIt is hard to give a better solution without the rest of your code, but there often are cleaner ways than for loops. One solution that avoids the character vector is to put the data frames together in list. Best regards, Josh> -- > View this message in context: http://r.789695.n4.nabble.com/use-a-data-frame-whose-name-is-stored-as-a-string-variable-tp2269095p2269095.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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Seth
2010-Jun-26 23:49 UTC
[R] use a data frame whose name is stored as a string variable?
Thanks! Works like a charm. -Seth -- View this message in context: http://r.789695.n4.nabble.com/use-a-data-frame-whose-name-is-stored-as-a-string-variable-tp2269095p2269732.html Sent from the R help mailing list archive at Nabble.com.