Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

dataframe - R list of lists to data.frame

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.

question from:https://stackoverflow.com/questions/29674661/r-list-of-lists-to-data-frame

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...