Hi all, I'm trying to avoid loops (no real reason, just as an exercise). Given a list: list(data.frame(a=1:3,b=letters[1:3]),data.frame(x=1:5,b=LETTERS[1:5])) Is there an easy way to collapse this to a single dataframe result<-data.frame(a=c(1:3,1:5),b=c(letters[1:3],LETTERS[1:5])) Thanks
How about this, t <- lapply(a,function(x){colnames(x)=c("A","B");return(x)}) do.call(rbind,t) On Mon, Feb 2, 2015 at 4:00 PM, Tom Wright <tom at maladmin.com> wrote:> Hi all, > > I'm trying to avoid loops (no real reason, just as an exercise). > > Given a list: > > list(data.frame(a=1:3,b=letters[1:3]),data.frame(x=1:5,b=LETTERS[1:5])) > > Is there an easy way to collapse this to a single dataframe > > result<-data.frame(a=c(1:3,1:5),b=c(letters[1:3],LETTERS[1:5])) > > > Thanks > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Thanks, After posting I came across this page: http://www.r-bloggers.com/concatenating-a-list-of-data-frames/ rbindlist seems to be a pretty good solution. On Mon, 2015-02-02 at 16:09 -0500, dan wang wrote:> How about this, > > > t <- lapply(a,function(x){colnames(x)=c("A","B");return(x)}) > do.call(rbind,t) > > > On Mon, Feb 2, 2015 at 4:00 PM, Tom Wright <tom at maladmin.com> wrote: > Hi all, > > I'm trying to avoid loops (no real reason, just as an > exercise). > > Given a list: > > list(data.frame(a=1:3,b=letters[1:3]),data.frame(x=1:5,b=LETTERS[1:5])) > > Is there an easy way to collapse this to a single dataframe > > result<-data.frame(a=c(1:3,1:5),b=c(letters[1:3],LETTERS[1:5])) > > > Thanks > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, > see > 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. > >