gpt4 book ai didi

r - expand.grid 与现有 block

转载 作者:行者123 更新时间:2023-12-04 04:43:17 24 4
gpt4 key购买 nike

嗨,晚上好,来自德国:)

我对 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”。 table 应该是这样的
   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"))

非常感谢,
马克

最佳答案

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

关于r - expand.grid 与现有 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18577905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com