【问题标题】:Most efficient list to data.frame method, when the list is a list of rows当列表是行列表时,最有效的列表到 data.frame 方法
【发布时间】:2012-02-04 13:09:21
【问题描述】:

This question 涵盖了我有一个列列表的情况,我希望将它们变成一个 data.frame。如果我有一个行列表,并且希望将它们转换为 data.frame,该怎么办?

rowList <- lapply(1:500000,function(x) sample(0:1,300,x))

解决这个问题的简单方法是使用rbindas.data.frame,但我们甚至无法通过rbind 步骤:

>Data <- do.call(rbind,vectorList)
Error: cannot allocate vector of size 572.2 Mb

这样做更有效率?

【问题讨论】:

  • @JoshuaUlrich -- 你确定它是重复的吗? Zach 询问rbind将一堆向量放在一起。您链接帖子中检查的方法似乎都是通过cbinding 或以其他方式组合列来构造data.frames。
  • @JoshO'Brien:好点。 Zach 需要澄清一下,特别是因为 do.call(rbind, vectorList) 会创建一个矩阵,而不是一个 data.frame。
  • @JoshuaUlrich:我编辑了我的帖子。如果需要进一步澄清,请告诉我。

标签: r memory-management data-management


【解决方案1】:

unlist 您的列表并填充矩阵可能是最快/最有效的:

> m <- matrix(unlist(vectorList), ncol=300, nrow=length(vectorList), byrow=TRUE)

但是您将需要 ~6GB 的 RAM 来处理整数向量和 ~12GB 的 RAM 来处理数字向量。

> l <- integer(5e6*300)
> print(object.size(l),units="Gb")
5.6 Gb

【讨论】:

    【解决方案2】:

    通过依赖 R 数组的列主要方面,尝试对矩阵进行直接强制:

    Data <- matrix(unlist(vectorList), ncol = length(vectorList[[1]]), byrow = TRUE)
    

    如果这也不起作用,您没有资源来复制这个东西,所以请考虑先创建矩阵并逐列填充它。

    【讨论】:

      猜你喜欢
      • 2011-08-22
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      相关资源
      最近更新 更多