+1 vote
2 views
in R Programming by (3.2k points)
edited by

Can someone please tell me a quick way to convert a nested list of data whose length is 100 and each item is a list of length 10 into a dataframe of 100 rows and 10 columns?

I am attaching a sample data:

5 <- replicate(

  132,

  list(sample(letters, 20)),

  simplify = FALSE

  )

1 Answer

0 votes
by (26.9k points)
edited by

This is a quick way to perform your task:

df <- data.frame(matrix(unlist(5), nrow=length(5), byrow=T))

This will convert all character columns to factors, just add a parameter to the data.frame() call to avoid this:

df <- data.frame(matrix(unlist(5), nrow=100 , byrow=T),stringsAsFactors=FALSE)

Alternatively you can also use rbind

do.call(rbind.data.frame, your_list)

There are other methods too but I think these two mentioned above will solve your problem.

...