Hello everyone! I have a list X with 3 elements, each of which is a data frame, for example: a<-data.frame(a=1,b=2,c=3) b<-data.frame(a=c(4,7),b=c(5,8),c=c(6,9)) c<-data.frame(a=c(10,13,16),b=c(11,14,17),c=c(12,15,18)) X<-list() X[[1]]<-a X[[2]]<-b X[[3]]<-c (X) How can I most effectively transform X into a data frame with columns a, b, and c? I would love to find a generic solution, and not a specific solution like this one: frame<-rbind(X[[1]],X[[2]],X[[3]]) ...because in reality I have a lot of elements of X. Thank you very much! -- Dimitri Liakhovitski MarketTools, Inc. Dimitri.Liakhovitski at markettools.com
On Apr 22, 2009, at 8:36 PM, Dimitri Liakhovitski wrote:> Hello everyone! > > I have a list X with 3 elements, each of which is a data frame, for > example: > > a<-data.frame(a=1,b=2,c=3) > b<-data.frame(a=c(4,7),b=c(5,8),c=c(6,9)) > c<-data.frame(a=c(10,13,16),b=c(11,14,17),c=c(12,15,18)) > X<-list() > X[[1]]<-a > X[[2]]<-b > X[[3]]<-c > (X) > > How can I most effectively transform X into a data frame with columns > a, b, and c? > I would love to find a generic solution, and not a specific solution > like this one: > > frame<-rbind(X[[1]],X[[2]],X[[3]]) > > ...because in reality I have a lot of elements of X. > > Thank you very much!see ?do.call > do.call(rbind, X) a b c 1 1 2 3 2 4 5 6 3 7 8 9 4 10 11 12 5 13 14 15 6 16 17 18 HTH, Marc Schwartz
?do.call ... as in do.call(rbind,X) (A very useful and powerful feature os the S language: Computing on the language. See V&R's "S Programming" for an informative discussion (there may well be others, of course). -- Bert -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Dimitri Liakhovitski Sent: Wednesday, April 22, 2009 6:36 PM To: R-Help List Subject: [R] rbind data frames stored in a list Hello everyone! I have a list X with 3 elements, each of which is a data frame, for example: a<-data.frame(a=1,b=2,c=3) b<-data.frame(a=c(4,7),b=c(5,8),c=c(6,9)) c<-data.frame(a=c(10,13,16),b=c(11,14,17),c=c(12,15,18)) X<-list() X[[1]]<-a X[[2]]<-b X[[3]]<-c (X) How can I most effectively transform X into a data frame with columns a, b, and c? I would love to find a generic solution, and not a specific solution like this one: frame<-rbind(X[[1]],X[[2]],X[[3]]) ...because in reality I have a lot of elements of X. Thank you very much! -- Dimitri Liakhovitski MarketTools, Inc. Dimitri.Liakhovitski at markettools.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.
'do.call' is your friend::> a<-data.frame(a=1,b=2,c=3) > b<-data.frame(a=c(4,7),b=c(5,8),c=c(6,9)) > c<-data.frame(a=c(10,13,16),b=c(11,14,17),c=c(12,15,18)) > X<-list() > X[[1]]<-a > X[[2]]<-b > X[[3]]<-c > do.call(rbind, X)a b c 1 1 2 3 2 4 5 6 3 7 8 9 4 10 11 12 5 13 14 15 6 16 17 18 On Wed, Apr 22, 2009 at 9:36 PM, Dimitri Liakhovitski <ld7631 at gmail.com> wrote:> Hello everyone! > > I have a list X with 3 elements, each of which is a data frame, for example: > > a<-data.frame(a=1,b=2,c=3) > b<-data.frame(a=c(4,7),b=c(5,8),c=c(6,9)) > c<-data.frame(a=c(10,13,16),b=c(11,14,17),c=c(12,15,18)) > X<-list() > X[[1]]<-a > X[[2]]<-b > X[[3]]<-c > (X) > > How can I most effectively transform X into a data frame with columns > a, b, and c? > I would love to find a generic solution, and not a specific solution > like this one: > > frame<-rbind(X[[1]],X[[2]],X[[3]]) > > ...because in reality I have a lot of elements of X. > > Thank you very much! > > -- > Dimitri Liakhovitski > MarketTools, Inc. > Dimitri.Liakhovitski at markettools.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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Replying to my own post. I found the following solution: full.frame<-as.data.frame(matrix(nrow=0,ncol=3)) names(full.frame)<-c("a","b","c") for(j in 1:length(X)) {full.frame<-rbind(full.frame,X[[j]])} (full.frame) Is there a more elegant solution? Thank you! Dimitri On Wed, Apr 22, 2009 at 9:36 PM, Dimitri Liakhovitski <ld7631 at gmail.com> wrote:> Hello everyone! > > I have a list X with 3 elements, each of which is a data frame, for example: > > a<-data.frame(a=1,b=2,c=3) > b<-data.frame(a=c(4,7),b=c(5,8),c=c(6,9)) > c<-data.frame(a=c(10,13,16),b=c(11,14,17),c=c(12,15,18)) > X<-list() > X[[1]]<-a > X[[2]]<-b > X[[3]]<-c > (X) > > How can I most effectively transform X into a data frame with columns > a, b, and c? > I would love to find a generic solution, and not a specific solution > like this one: > > frame<-rbind(X[[1]],X[[2]],X[[3]]) > > ...because in reality I have a lot of elements of X. > > Thank you very much! > > -- > Dimitri Liakhovitski > MarketTools, Inc. > Dimitri.Liakhovitski at markettools.com >-- Dimitri Liakhovitski MarketTools, Inc. Dimitri.Liakhovitski at markettools.com
Reasonably Related Threads
- Question about multiple regression
- Analogy for %in% for the whole columns (rather than individual values)
- changing settings on a barchart (lattice)
- interaction.plot - gridlines and formatting legend title...
- tapply bug? - levels of a factor in a data frame after tapply are intermixed