[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:02:52 CEST 2011


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



More information about the R-help mailing list