Jie Li
2010-Sep-13 18:40 UTC
[R] How to tell R that "ABC" is the name of a dataset not a variable value
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]]
Henrique Dallazuanna
2010-Sep-13 20:24 UTC
[R] How to tell R that "ABC" is the name of a dataset not a variable value
Se ?get function. On Mon, Sep 13, 2010 at 3:40 PM, Jie Li <rusers18@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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Joshua Wiley
2010-Sep-13 20:35 UTC
[R] How to tell R that "ABC" is the name of a dataset not a variable value
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/