Mark Ebbert
2011-Aug-31 20:02 UTC
[R] Convert List of Data.Frames to Data.Frame when List has NULL
Dear R gurus, I trying to convert a list of data frames to one data frame, but one of the values within the list is NULL, so I get the error "arguments imply differing number of rows." Do you know of a quick way to tell R to combine all that are not null? Here is a simple example:> tmp<-list(l1=data.frame(col1=c(1,2,3),col2=c(2,3,4)),l2=NULL,l3=data.frame(col1=c(3,4,5),col2=c(4,5,6))) > mtpError: object 'mtp' not found> tmp$l1 col1 col2 1 1 2 2 2 3 3 3 4 $l2 NULL $l3 col1 col2 1 3 4 2 4 5 3 5 6> tmp.d<-as.data.frame(tmp)Error in data.frame(l1 = list(col1 = c(1, 2, 3), col2 = c(2, 3, 4)), l2 = NULL, : arguments imply differing number of rows: 3, 0 I have also tried doing "do.call("cbind",tmp)" I appreciate your help! Mark
Jorge I Velez
2011-Aug-31 20:18 UTC
[R] Convert List of Data.Frames to Data.Frame when List has NULL
Hi Mark, Try do.call(rbind, tmp[!sapply(tmp, is.null)]) HTH, Jorge On Wed, Aug 31, 2011 at 4:02 PM, Mark Ebbert <> wrote:> Dear R gurus, > > I trying to convert a list of data frames to one data frame, but one of the > values within the list is NULL, so I get the error "arguments imply > differing number of rows." Do you know of a quick way to tell R to combine > all that are not null? Here is a simple example: > > > > tmp<-list(l1=data.frame(col1=c(1,2,3),col2=c(2,3,4)),l2=NULL,l3=data.frame(col1=c(3,4,5),col2=c(4,5,6))) > > mtp > Error: object 'mtp' not found > > tmp > $l1 > col1 col2 > 1 1 2 > 2 2 3 > 3 3 4 > > $l2 > NULL > > $l3 > col1 col2 > 1 3 4 > 2 4 5 > 3 5 6 > > > tmp.d<-as.data.frame(tmp) > Error in data.frame(l1 = list(col1 = c(1, 2, 3), col2 = c(2, 3, 4)), l2 > NULL, : > arguments imply differing number of rows: 3, 0 > > I have also tried doing "do.call("cbind",tmp)" > > I appreciate your help! > > Mark > > ______________________________________________ > 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. >[[alternative HTML version deleted]]