[R] How to tell R that "ABC" is the name of a dataset not a variable value

Joshua Wiley jwiley.psych at gmail.com
Mon Sep 13 22:35:16 CEST 2010


Hi,

Just to provide an example:


#Sample data copied to variables "X", "Y", and "Z"
Z <- Y <- X <- data.frame(A = 1:10)

# A variable holding the names of the variables
datasetname <- c("X", "Y", "Z")

# Use mget() to collect all the variables in a list
# get() only gets one variable at a t ime, mget() gets multiple
# however, mget() needs to know which environment to look in
# If your variables are just in your usual workspace
# it should be .GlobalEnv
alldata <- mget(x = datasetname, envir = .GlobalEnv)

HTH,

Josh


On Mon, Sep 13, 2010 at 11:40 AM, Jie Li <rusers18 at gmail.com> wrote:
> All,
>
> For example, I have a dataset named "ABC" loaded into R
>> ABC
>     [,1] [,2] [,3]
> [1,]    1    4    7
> [2,]    2    5    8
> [3,]    3    6    9
>
> and I also have a variable datasetname
>> datasetname
> [1] "ABC"
>
> and I want to add this "ABC" dataset to an existing list "alldata"
>
>  > alldata <-NULL
>> alldata <- as.list(alldata)
>
> since I will have lots of datasets like "ABC" loaded to R, and have
> datasetname variable record their names, I want to add all the datasets to
> the list alldata by just writing one command. But I don't know to tell R
> that "ABC" is a dataset's name instead of a value of variable datasetname?
> Thank you all for your help!
>
> JL
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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/



More information about the R-help mailing list