Convert a list to data.frame with multiple columns in R

LDT

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

akrun

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

data

lst1 <- list(1:2, c(10, 20, 30), 4)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert columns a data frame to a list in R

Convert columns in a list to a data frame in R

how do you convert data frame to json with multiple columns in R

Convert matrix column in R data frame to multiple columns

Convert multiple columns of a data frame from string to numeric in R

Convert all data frame's columns to multiple vectors in R

How to parse a list of multiple list and convert it to data frame in R?

R: applying a function that returns a list across multiple columns of a data frame

R convert list with multiple string lengths to data frame

convert a list to data frame in R

Convert string into float for multiple columns in data frame

Convert pandas data frame (with multiple columns) to series

convert multiple data frame columns to numeric vector

R - Convert list of list into data frame

Convert list of multiple strings into a Python data frame

Convert Survey list object into data frame and split string column into multiple columns

R data frame - aggregating multiple columns at once

Unlisting multiple columns in R data frame

How to merge data frame with multiple columns with R?

replace multiple columns of data frame with indexing in r

Recoding multiple columns in a R data frame

Compare data frame multiple columns with row in R

Looking to transpose a data frame on multiple columns in R

R data.frame columns into single list

R: "Filter" columns in a data frame by a list

Split Data Frame into a List of Subsets of Columns in R

Create a data frame with the common columns of a data frame list - R

How to convert a list of tables to a data frame in R

Convert a data frame to a list with proper label in R