R列表到数据帧 [英] R list to data frame

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

问题描述

我有一个嵌套的数据列表。它的长度为132,每个项目是长度为20的列表。是否有一种将此结构转换为具有132行和20列数据的数据框架的快速方式?



我是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屋!

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