【问题标题】:Unique combinations of all variables所有变量的独特组合
【发布时间】:2014-12-03 06:20:09
【问题描述】:

我尝试使用以下代码来生成一组变量的唯一组合表。

V1=as.vector(CRmarch30[1])
V2=as.vector(CRmarch30[2])
V3=as.vector(CRmarch30[3])
V4=as.vector(CRmarch30[4])
V5=as.vector(CRmarch30[5])
V6=as.vector(CRmarch30[6])
V7=as.vector(CRmarch30[7])

您可能已经猜到了,CRmarch30 是一个包含 7 列的数据框。我将每一列转换为一个向量。然后,我使用以下代码创建了 7 个变量的所有唯一组合:

combo=expand.grid(V1,V2,V3,V4,V5,V6,V7)
combo

我没有得到输出,而是得到以下错误消息:

 Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 错误:无法分配大小为 512001.3 Gb 的向量另外:警告消息:1:在 rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)) , orep) : 达到 8089Mb 的总分配量: 见 help(memory.size) 2: 在 rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : 达到总数8089Mb 的分配:参见帮助(memory.size) 3:在 rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) 中:达到 8089Mb 的总分配:参见帮助(memory.size) 4:在 rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) 中:达到 8089Mb 的总分配:请参阅 help(memory.size)

标签: r unique combinations


【解决方案1】:

as.vector 未将其转换为 vector 例如:

V1=as.vector(CRmarch30[1])
V2=as.vector(CRmarch30[2])
 V3=as.vector(CRmarch30[3])

expand.grid(V1, V2, V3)
#  Var1 Var2 Var3
#1    1    5    0
#Warning message:
#In format.data.frame(x, digits = digits, na.encode = FALSE) :
# corrupt data frame: columns will be truncated or padded with NAs

 is.vector(V1)
 #[1] FALSE
 is.data.frame(CRmarch30[1])
 #[1] TRUE

你本来可以的

 V1 <- CRmarch30[,1]
 is.vector(V1)
 #[1] TRUE

但是,您不需要创建 vector 对象。这可以通过(如果您需要unique 组合)来完成

 do.call(expand.grid,lapply(CRmarch30,unique))

或者如果列已经是unique

 do.call(expand.grid, CRmarch30)

或使用data.table

 library(data.table)
 setDT(CRmarch30)[,do.call(CJ, lapply(.SD, unique))]

数据

set.seed(22)
CRmarch30 <- as.data.frame(matrix(sample(c(NA,0:5), 10*3,
                                    replace=TRUE), ncol=3))

【讨论】:

  • 非常感谢阿克伦。当我尝试使用 do.call(expand.grid,lappply(CRmarch30,unique)) 时,出现错误“错误:无法分配大小为 512001.3 Gb 的向量另外:警告消息:1:在 rep.int(rep. int(seq_len(nx), rep.int(rep.fac, nx)), orep) :达到 8089Mb 的总分配:参见 help(memory.size) 2:在 rep.int(rep.int(seq_len(nx) , rep.int(rep.fac, nx)), orep) :达到 8089Mb 的总分配:请参阅 help(memory.size) 3:在 rep.int(rep.int(seq_len(nx), rep.int(rep) .fac, nx)), orep) : 达到 8089Mb 的总分配量:参见帮助(memory.size)
  • @user3007275 看起来您有一个大型数据集。通过在如此大的数据集上执行expand.grid 也将要求很高。或许,你可以在内存更好的系统上试试,或者使用clusters,或者数据库等。
  • 感谢 akrun。是的,我有 1500 个观察值。我还有另一个包含 16000 个观察值的数据集,我也需要为该数据集提出类似的组合。反正有在 R 或 excel 中做吗?集群如何满足我的需求?
  • @user3007275 我的意思是系统的cluster。如果您在大学工作/学习,他们可能有集群,您可以在其中运行它。
  • 是否可以在 R 中运行它,这样它就不必显示输出,而是将该输出存储为数据集/数据框并且可以导出到 excel ?我猜这个错误是由于 R 运行代码并试图生成输出?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
相关资源
最近更新 更多