Skip to content

Convert List of Data.Frames to Data.Frame when List has NULL

3 messages · Mark Ebbert, Jorge I Velez

#
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:
Error: object 'mtp' not found
$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
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
#
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:
Error: object 'mtp' not found
$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
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.