What I am trying to do is as follows: - I have listed names of all wanted objects (datasets A,B,C... ) in current workspace as a vector: obj <- c('A','B','C') - then i need to use these objects, say to extract all the 1st columns and bind to an existing dataset ('data'): for ( i in 1:3){ newdata <- obj[i] data <- cbind(data,newdata [[1]] ) } Obviously, it doesn't work since obj[i] is just a string of dataset name. Here is my question: how to call it as a original dataset? Thanks. -- View this message in context: http://www.nabble.com/enable-object-name-to-be-called-as-object-%28a-dataset%29-tf4403933.html#a12563767 Sent from the R help mailing list archive at Nabble.com.
Gabor Csardi
2007-Sep-07 22:04 UTC
[R] enable object name to be called as object (a dataset)
"get" might be good enough for you:> a <- 10 > name <- "a" > get("a")[1] 10> get(name)[1] 10>Gabor On Fri, Sep 07, 2007 at 02:42:07PM -0700, runner wrote:> > What I am trying to do is as follows: > > - I have listed names of all wanted objects (datasets A,B,C... ) in current > workspace as a vector: > > obj <- c('A','B','C') > > - then i need to use these objects, say to extract all the 1st columns and > bind to an existing dataset ('data'): > > for ( i in 1:3){ > newdata <- obj[i] > data <- cbind(data,newdata [[1]] ) > } > > Obviously, it doesn't work since obj[i] is just a string of dataset name. > Here is my question: how to call it as a original dataset? Thanks. > -- > View this message in context: http://www.nabble.com/enable-object-name-to-be-called-as-object-%28a-dataset%29-tf4403933.html#a12563767 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Csardi Gabor <csardi at rmki.kfki.hu> MTA RMKI, ELTE TTK
Giovanni Petris
2007-Sep-07 22:10 UTC
[R] enable object name to be called as object (a dataset)
This should work: do.call(cbind, lapply(1:length(obj), function(i) get(obj[i])[,1])) Best, Giovanni> Date: Fri, 07 Sep 2007 14:42:07 -0700 (PDT) > From: runner <sunnyside500 at gmail.com> > Sender: r-help-bounces at stat.math.ethz.ch > Precedence: list > > > What I am trying to do is as follows: > > - I have listed names of all wanted objects (datasets A,B,C... ) in current > workspace as a vector: > > obj <- c('A','B','C') > > - then i need to use these objects, say to extract all the 1st columns and > bind to an existing dataset ('data'): > > for ( i in 1:3){ > newdata <- obj[i] > data <- cbind(data,newdata [[1]] ) > } > > Obviously, it doesn't work since obj[i] is just a string of dataset name. > Here is my question: how to call it as a original dataset? Thanks. > -- > View this message in context: http://www.nabble.com/enable-object-name-to-be-called-as-object-%28a-dataset%29-tf4403933.html#a12563767 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >-- Giovanni Petris <GPetris at uark.edu> Associate Professor Department of Mathematical Sciences University of Arkansas - Fayetteville, AR 72701 Ph: (479) 575-6324, 575-8630 (fax) http://definetti.uark.edu/~gpetris/
a <- 1:3 b <- 11:13 c <- 21:23 names <- c('a','b','c') do.call(data.frame, list(sapply(names, function(x) get(x)))) runner wrote:> > What I am trying to do is as follows: > > - I have listed names of all wanted objects (datasets A,B,C... ) in > current workspace as a vector: > > obj <- c('A','B','C') > > - then i need to use these objects, say to extract all the 1st columns and > bind to an existing dataset ('data'): > > for ( i in 1:3){ > newdata <- obj[i] > data <- cbind(data,newdata [[1]] ) > } > > Obviously, it doesn't work since obj[i] is just a string of dataset name. > Here is my question: how to call it as a original dataset? Thanks. >-- View this message in context: http://www.nabble.com/enable-object-name-to-be-called-as-object-%28a-dataset%29-tf4403933.html#a12564175 Sent from the R help mailing list archive at Nabble.com.