do.call(rbind, list) for uneven number of column

Posted by h.l.m on Stack Overflow See other posts from Stack Overflow or by h.l.m
Published on 2013-06-25T22:19:46Z Indexed on 2013/06/25 22:21 UTC
Read the original article Hit count: 207

Filed under:
|
|

I have a list, with each element being a character vector, of differing lengths I would like to bind the data as rows, so that the column names 'line up' and if there is extra data then create column and if there is missing data then create NAs

Below is a mock example of the data I am working with

x <- list()
x[[1]] <- letters[seq(2,20,by=2)]
names(x[[1]]) <- LETTERS[c(1:length(x[[1]]))]
x[[2]] <- letters[seq(3,20, by=3)]
names(x[[2]]) <- LETTERS[seq(3,20, by=3)]
x[[3]] <- letters[seq(4,20, by=4)]
names(x[[3]]) <- LETTERS[seq(4,20, by=4)]

The below line would normally be what I would do if I was sure that the format for each element was the same...

do.call(rbind,x)

I was hoping that someone had come up with a nice little solution that matches up the column names and fills in blanks with NAs whilst adding new columns if in the binding process new columns are found...

© Stack Overflow or respective owner

Related posts about r

    Related posts about list