2

嗨,来自德国的晚安:)

我对 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 例如,'split's 不应该超过 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"))

非常感谢,马克

4

1 回答 1

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

如果您有更多列,则可以使用lapplyanddo.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],]))
于 2013-09-02T17:22:07.083 回答