【问题标题】:expand.grid with existing chunksexpand.grid 与现有块
【发布时间】:2013-09-03 01:49:02
【问题描述】:

来自德国的你好,晚上好 :)

我对 R 很陌生,但我真的在我的理解范围内。

基本上我有 n 个矩阵,它们在一个列表中。它们看起来像这样:

 $edu
   cue op split  pred
1  edu  <     1  TRUE
2  edu  >     1  TRUE
3  edu ==     1  TRUE
4  edu  <     2  TRUE
5  edu  >     2  TRUE
6  edu ==     2  TRUE
7  edu  <     3  TRUE
8  edu  >     3  TRUE
9  edu ==     3  TRUE

$religion
       cue op split  pred
1 religion ==     0  TRUE
2 religion ==     1  TRUE
3 religion ==     0 FALSE
4 religion ==     1 FALSE

$med_exp
       cue op split  pred
1 med_exp  ==     0  TRUE
2 med_exp  ==     1  TRUE
3 med_exp  ==     0 FALSE
4 med_exp  ==     1 FALSE

我需要的是类似于“expand.grid()”所做的事情。我需要以所有可能的排列方式将所有项目混合在一起(我已经检查了 'combinat' 包),但是按照它们的原始列顺序(med_exp 例如不应该有超过 1 的 'split's 并且只有“==”作为运算符!) .

如果我在表格中不止一次出现相同的“cuetest”(一个列表中的一行),我会适得其反。我最好需要矩阵中的数据,因为我想使用“parRapply”。桌子应该是这样的

   cue op split  pred      cue op split  pred     cue  op split  pred
   edu  <     1  TRUE religion ==     0  TRUE med_exp  ==     0  TRUE
   edu  <     1  TRUE religion ==     0  TRUE med_exp  ==     1  TRUE
   edu  <     1  TRUE religion ==     0  TRUE med_exp  ==     0  FALSE
   edu  <     1  TRUE religion ==     0  TRUE med_exp  ==     1  FALSE

    [..]
    med_exp  == 0 TRUE edu       <     1  TRUE religion ==     0  TRUE

问题是,expand.grid 甚至会混淆子列,这只会给我带来记忆问题,而且没有必要。

以下是要试验的数据:

structure(list(edu = structure(list(cue = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = "edu", class = "factor"), op = structure(c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("<", ">", "=="), class = "factor"), 
    split = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 
    1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), pred = c(TRUE, 
    TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
    TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE)), .Names = c("cue", "op", "split", 
"pred"), out.attrs = structure(list(dim = c(1L, 3L, 4L, 2L), 
    dimnames = structure(list(Var1 = "Var1=edu", Var2 = c("Var2=<", 
    "Var2=>", "Var2==="), Var3 = c("Var3=1", "Var3=2", "Var3=3", 
    "Var3=4"), Var4 = c("Var4=TRUE", "Var4=FALSE")), .Names = c("Var1", 
    "Var2", "Var3", "Var4"))), .Names = c("dim", "dimnames")), class = "data.frame", row.names = c(NA, 
-24L)), edu_hus = structure(list(cue = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = "edu_hus", class = "factor"), op = structure(c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("<", ">", "=="), class = "factor"), 
    split = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 
    1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), pred = c(TRUE, 
    TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
    TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE)), .Names = c("cue", "op", "split", 
"pred"), out.attrs = structure(list(dim = c(1L, 3L, 4L, 2L), 
    dimnames = structure(list(Var1 = "Var1=edu_hus", Var2 = c("Var2=<", 
    "Var2=>", "Var2==="), Var3 = c("Var3=1", "Var3=2", "Var3=3", 
    "Var3=4"), Var4 = c("Var4=TRUE", "Var4=FALSE")), .Names = c("Var1", 
    "Var2", "Var3", "Var4"))), .Names = c("dim", "dimnames")), class = "data.frame", row.names = c(NA, 
-24L)), religion = structure(list(cue = structure(c(1L, 1L, 1L, 
1L), .Label = "religion", class = "factor"), op = structure(c(1L, 
1L, 1L, 1L), .Label = "==", class = "factor"), split = c(0L, 
1L, 0L, 1L), pred = c(TRUE, TRUE, FALSE, FALSE)), .Names = c("cue", 
"op", "split", "pred"), out.attrs = structure(list(dim = c(1L, 
1L, 2L, 2L), dimnames = structure(list(Var1 = "Var1=religion", 
    Var2 = "Var2===", Var3 = c("Var3=0", "Var3=1"), Var4 = c("Var4=TRUE", 
    "Var4=FALSE")), .Names = c("Var1", "Var2", "Var3", "Var4"
))), .Names = c("dim", "dimnames")), class = "data.frame", row.names = c(NA, 
-4L))), .Names = c("edu", "edu_hus", "religion"))

非常感谢, 马克

【问题讨论】:

  • 那么你的问题到底是什么?
  • 对不起,如果不够清楚。我需要一个“expand.grid”,它不会破坏我在数据框中已有的计算,而是重新组合所有列表元素中的 DF 的所有行。请参阅@Roland 的回答,这就像一个魅力。

标签: r permutation


【解决方案1】:

请注意dput提供的数据与前面显示的不一样。此外,列表的元素是 data.frames 而不是矩阵。前者是有道理的,因为您混合了不同的类型(因子、整数、逻辑),而矩阵只能保存一种类型。因此,我在这里给出的代码也返回了一个 data.frame。您始终可以在其上使用as.matrix,但我建议在大多数情况下不要这样做。

#create combinations of row indices
ind <- expand.grid(seq_len(nrow(dat[[3]])), 
                   seq_len(nrow(dat[[2]])), 
                   seq_len(nrow(dat[[1]])))

#use subsetting and cbind
res <- cbind(dat[[1]][ind[,3],],
              dat[[2]][ind[,2],], 
              dat[[3]][ind[,1],])

head(res)

#     cue op split pred     cue op split pred      cue op split  pred
# 1   edu  <     1 TRUE edu_hus  <     1 TRUE religion ==     0  TRUE
# 1.1 edu  <     1 TRUE edu_hus  <     1 TRUE religion ==     1  TRUE
# 1.2 edu  <     1 TRUE edu_hus  <     1 TRUE religion ==     0 FALSE
# 1.3 edu  <     1 TRUE edu_hus  <     1 TRUE religion ==     1 FALSE
# 1.4 edu  <     1 TRUE edu_hus  >     1 TRUE religion ==     0  TRUE
# 1.5 edu  <     1 TRUE edu_hus  >     1 TRUE religion ==     1  TRUE

如果你有更多的列,你可以使用lapplydo.call来概括方法:

sl <- lapply(dat, function(df) seq_len(nrow(df)))
sl <- sl[rev(seq_along(sl))]
ind <- do.call(expand.grid, sl)
ind <- ind[,rev(seq_along(dat))]

res <- do.call(cbind, lapply(seq_along(dat), function(i) dat[[i]][ind[,i],]))

【讨论】:

  • 哇!这太神奇了,而且速度惊人!非常感谢您的解释。我还没有得到你的全部代码,但我正在努力。干杯,谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-06-22
  • 2016-05-24
  • 1970-01-01
  • 1970-01-01
  • 2015-04-24
  • 2017-10-13
  • 1970-01-01
  • 2013-10-24
相关资源
最近更新 更多