将列表转换为数据框 [英] Convert a list to a data frame

查看:76
本文介绍了将列表转换为数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的数据列表.它的长度为132,每个项目都是一个长度为20的列表.是否存在快速方法将该结构转换为具有132行和20列数据的数据帧?

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?

以下是一些示例数据可以使用:

Here is some sample data to work with:

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

推荐答案

假设您的列表列表称为l:

Assuming your list of lists is called l:

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

上面的代码会将所有字符列转换为因子,为避免这种情况,您可以在data.frame()调用中添加一个参数:

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)

这篇关于将列表转换为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆