[R] Convert List of Data.Frames to Data.Frame when List has NULL

Mark Ebbert Mark.Ebbert at hci.utah.edu
Wed Aug 31 22:45:49 CEST 2011


Yeah, I never would have come up with that one. Thanks!


On Aug 31, 2011, at 2:18 PM, Jorge I Velez wrote:

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 at r-project.org<mailto: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.



More information about the R-help mailing list