expand.grid与现有的块

时间:2013-09-02 17:03:11

标签: r permutation

嗨,晚上来自德国:)

我对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且只有“==”作为运算符!) 。

如果我在表格中多次使用相同的“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"))

非常感谢, 马克

1 个答案:

答案 0 :(得分:3)

请注意,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],]))