Mayou
Mayou

Reputation: 8848

Converting list of varying-length elements to dataframe

Consider the following list of vector elements with varying length:

test = list(c(A = 1, B = 2), c(A = 3, C = 1), c(A = 9), c(A = 6, B = 7, C = 8))

I would like to convert the list to a dataframe, while matching the names of the elements in the following fashion:

 #  A  B  C
 #  1  2 NA
 #  3 NA  1
 #  9 NA NA
 #  6  7  8

Upvotes: 3

Views: 364

Answers (1)

eddi
eddi

Reputation: 49448

library(plyr)

rbind.fill(lapply(test, function(x) as.data.frame(t(x))))
#  A  B  C
#1 1  2 NA
#2 3 NA  1
#3 9 NA NA
#4 6  7  8

Upvotes: 4

Related Questions