I have a list that looks like this
[[1]]
1 2
[[2]]
10 20 30
[[3]]
4
and I would like to convert it to a dataframe that looks like this
column1 column2 column3
1 2 NA
10 20 30
4 NA NA
Any help is highly appreciated
An option is to get the lengths
of the list
elements, get the max
length, pad NA at the end where the length is less than the max
length and dbind
the list
elements
mx <- max(lengths(lst1))
out <- do.call(rbind.data.frame, lapply(lst1, `length<-`, mx))
names(out) <- paste0('column', 1:3)
out
# column1 column2 column3
#1 1 2 NA
#2 10 20 30
#3 4 NA NA
lst1 <- list(1:2, c(10, 20, 30), 4)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments