嗨,来自德国的晚安:)
我对 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"))
非常感谢,马克