Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R list of lists to data.frame

Tags:

dataframe

r

I've got a list of lists, call it listHolder, which has length 5.

Every element in listHolder is a list of numeric data, with 160 or so elements.

I need to turn this list of lists into a data.frame of length 5, with each element being a numeric vector with 160 or so elements.

But everything I've tried, from iterating through the list of lists and turning each element with as.numeric(unlist(listHolder[[i]])), to

data.frame(matrix(unlist(listHolder), nrow = length(totalKeywords), byrow = T)) 

ends up creating a data frame of length 160 or so, with each element being a numeric vector with 5 or so elements.

How do I do what I want?

Attempting data.frame(matrix(unlist(totalKeywords), nrow=132, byrow=T)) yields the opposite of what I want - 160 small items each 5 elements long.

like image 513
Bacter Avatar asked Apr 16 '15 12:04

Bacter


People also ask

How do I turn a list into a Dataframe in R?

To convert List to Data Frame in R, call as. data. frame() function and pass the list as argument to it.

Can a list contain data frame in R?

DataFrames are generic data objects of R which are used to store the tabular data. They are two-dimensional, heterogeneous data structures. A list in R, however, comprises of elements, vectors, data frames, variables, or lists that may belong to different data types.


1 Answers

AS @dimitris_ps mentioned earlier, the answer could be:

do.call(rbind, listHolder) 

Since do.call naturally "strips" 1 level of the "list of list", obtaining a list, not a list of lists.

After that, rbind can handle the elements on the list and create a matrix.

like image 130
Camilo Abboud Avatar answered Sep 19 '22 06:09

Camilo Abboud