Btibert3 Btibert3 - 1 year ago 93
R Question

R list to data frame

I have a nested list of data. Its length is 132 and each item is a list of length 20. Is there a quick way to convert this structure into a data frame that has 132 rows and 20 columns of data?

I am new to R, so I figure there is probably an easy way to do this. I searched here on Stack Overflow and couldn’t find a similar question, so I apologize if I missed it. Some sample data:

l <- replicate(
list(sample(letters, 20)),
simplify = FALSE

Answer Source

Assuming your list of lists is called l:

df <- data.frame(matrix(unlist(l), nrow=132, byrow=T))

The above will convert all character columns to factors, to avoid this you can add a parameter to the data.frame() call:

df <- data.frame(matrix(unlist(l), nrow=132, byrow=T),stringsAsFactors=FALSE)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download