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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Seth
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
Reply | Threaded
Open this post in threaded view
|

Re: use a data frame whose name is stored as a string variable?

Joshua Wiley-2
On Fri, Jun 25, 2010 at 5:10 PM, Seth <[hidden email]> 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 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
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.
>
> ______________________________________________
> [hidden email] 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/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: use a data frame whose name is stored as a string variable?

Seth
Thanks!  Works like a charm.  -Seth