3

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

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

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

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

这样做更有效率吗?

4

2 回答 2

5

它可能对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
于 2012-01-06T21:17:38.290 回答
1

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

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

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

于 2012-01-06T21:18:34.120 回答