6

I have a list of data frames where some data frames have less columns. See example:

a <- data.frame (x1 = 1, x3 = 2, x4 = 7)
b <- data.frame (x1 = 3, x2 = 4, x3 = 3, x4 = 8)
c <- data.frame (x1 = 9, x2 = 5, x3 = 2, x4 = 9)
myList = list(a, b, c)

data frame a misses column x2. I need to have a data frame out of myList, so I do the following:

mydf = do.call(rbind, myList)

But the problem is that I get the following error:

Error in rbind(deparse.level, ...) : 
  numbers of columns of arguments do not match

How can I get a data frame where column x2 for a is filled with NA?

Bruno Guarita
  • 767
  • 10
  • 21

1 Answers1

11

You can use data.table:

library(data.table)
rbindlist(myList, fill = TRUE)
#   x1 x3 x4 x2
#1:  1  2  7 NA
#2:  3  3  8  4
#3:  9  2  9  5
LyzandeR
  • 37,047
  • 12
  • 77
  • 87