[R] use a data frame whose name is stored as a string variable?

Joshua Wiley jwiley.psych at gmail.com
Sat Jun 26 02:18:55 CEST 2010

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?


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 Myers

It 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

Best regards,


> --
> 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

More information about the R-help mailing list