R列表到数据帧 [英] R list to data frame
问题描述
我是R的新手,所以我认为这可能是一个简单的方法。我在这个堆栈溢出搜索,找不到类似的问题,所以我道歉,如果我错过了。一些样本数据:
l < - replicate(
132,
list(sample(letters, 20)),
simplified = FALSE
)
假设您的列表列表被称为 l
:
df< - data.frame(matrix(unlist(l),nrow = 132,byrow = T))
以上将会将所有字符列转换为因子,以避免这种情况,您可以在data.frame()调用中添加一个参数:
df< - data.frame(matrix(unlist(l),nrow = 132,byrow = T),stringsAsFactors = FALSE)
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(
132,
list(sample(letters, 20)),
simplify = FALSE
)
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)
这篇关于R列表到数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!