【问题标题】:"Cannot allocate vector size" error when trying to merge two data frames尝试合并两个数据帧时出现“无法分配向量大小”错误
【发布时间】:2016-03-04 02:33:17
【问题描述】:

我有两个数据框,每个都有 120 万行。

我尝试将它们与dummy <- merge(df1, df2) 合并。两个数据框都没有共同的列,但两个数据框的行都按我想要的顺序排列。

我希望它们并排合并,但是当我运行合并功能时,我得到了这个错误:

    Error: cannot allocate vector of size 5905.6 Gb
In addition: Warning messages:
1: In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
  Reached total allocation of 8107Mb: see help(memory.size)
2: In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
  Reached total allocation of 8107Mb: see help(memory.size)
3: In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
  Reached total allocation of 8107Mb: see help(memory.size)
4: In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
  Reached total allocation of 8107Mb: see help(memory.size)

【问题讨论】:

  • 什么是object.size(df1)object.size(df2)
  • (120 万行告诉我你应该使用data.table,顺便说一句......)
  • “并排合并”:5905.6 Gb。真的没有希望完成这项工作。我怀疑您正在合并少量列,并且有很多重复值。对于两个数据集中重复值的每个组合,笛卡尔连接中有 N x M 个匹配项。请重新考虑您的方法。如果“并排”表示“逐行”,那么您需要cbind,而不是merge。问题应该是dim(df1)dim(df2) 是什么。
  • 矩阵和 data.frames 都有 cbind 方法。将cbind 与数据帧一起使用并没有特别危险,这类似于在数据帧上使用apply 的危险。不会发生对“矩阵”类的强制。对于可以确保“并行数据”的情况,有一个merge( , , by="row.names")。)
  • @MichaelChirico 你完全正确。其他阅读本文的人必须小心。就我而言,我肯定知道两个数据集中的行排成一行。我希望我能说更多关于我是如何知道的,但我不能。

标签: r merge


【解决方案1】:

您可能想尝试 data.table::cbind 以作为通过引用替代在 row.names 上合并。

library("data.table")
setDT(df1)
setDT(df2)
data.table::cbind(df1, df2)

但请注意包中的此警告reference

这些函数在 data.table 中被屏蔽了,因为在 绑定:

“如果至少有一个参数是数据,将使用数据框方法 框架”。这意味着 cbind(DT,DF) 调度到 S3 方法 cbind.data.frame 即使 cbind.data.table 由 data.table 提供。 因此,我们屏蔽了这些功能。警告信息将 加载 data.table 包时呈现给用户(由 R)。如果 第一个参数不是 data.table,然后它们恢复为基数 版本。

【讨论】:

  • 根据数据的结构和密度,您可能还会考虑将这些数据帧转换为稀疏矩阵并使用类似cBind
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 2022-11-28
  • 1970-01-01
  • 2021-12-04
  • 2022-07-13
  • 1970-01-01
  • 2010-11-19
相关资源
最近更新 更多