将大型列表转换为数据框的快速方式 [英] Fast way of converting large list to dataframe

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

问题描述

我有一个巨大的列表(700个元素),每个元素是长度= 16,000的向量。
我正在寻找一种将列表转换为数据框的有效方式,以下列方式(这只是一个模拟示例):

  lst < -  list(a = c(1,2,3),b = c(4,5,6),c = c(7,8,9))

我正在寻找的最终结果是:

 #[,1] [,2] [,3] 
#a 1 2 3
#b 4 5 6
#c 7 8 9

这是我尝试过的,但不按我的意愿工作:

  library(data.table)
result = rbindlist(Map(as.data.frame,lst))

任何建议?请记住,我的真实例子有很大的维度,我需要一个相当有效的方法来执行此操作。



非常感谢!

解决方案

尝试这个。我们假设 L 的组件都具有相同的长度, n ,我们也假设没有行名: / p>

  L<  -  list(a = 1:4,b = 4:1)#test input 

n< - length(L [[1]])
DF< - 结构(L,row.names = c(NA,-n),class =data.frame)


I have a huge list (700 elements), each element being a vector of length = 16,000. I am looking for an efficient way of converting the list to a dataframe, in the following fashion (this is just a mock example):

lst <- list(a = c(1,2,3), b = c(4,5,6), c = c(7,8,9))

The end result I am looking for is:

 #  [,1] [,2] [,3]
 #a    1    2    3
 #b    4    5    6
 #c    7    8    9

This is what I have tried, but isn't working as I wish:

library(data.table)
result = rbindlist(Map(as.data.frame, lst))

Any suggestion? Please keep in mind that my real example has huge dimensions, and I would need a rather efficient way of doing this operation.

Thank you very much!

解决方案

Try this. We assume the components of L all are of the same length, n, and we also assume no row names:

L <- list(a = 1:4, b = 4:1) # test input

n <- length(L[[1]])
DF <- structure(L, row.names = c(NA, -n), class = "data.frame")

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

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