有没有更优雅的方法来做到这一点?a 和 b 并行运行,c 和 d 形成笛卡尔积
a <- c("a","b","c")
b <- c("A","B","C")
c <- c(1,2,3)
d <- c(11,22)
nrow_cd <- length(c)*length(d)
nrow_abcd <- length(a)*nrow_cd
abcd <- data.frame(
a=rep(a, each=nrow_cd),
b=rep(b, each=nrow_cd),
c=rep(c, each=length(d)),
d=rep(d, times=nrow_abcd)
)
abcd
a b c d
1 a A 1 11
2 a A 1 22
3 a A 2 11
4 a A 2 22
5 a A 3 11
6 a A 3 22
7 b B 1 11
8 b B 1 22
9 b B 2 11
10 b B 2 22
11 b B 3 11
12 b B 3 22
13 c C 1 11
14 c C 1 22
15 c C 2 11
16 c C 2 22
17 c C 3 11
18 c C 3 22
19 a A 1 11
20 a A 1 22
21 a A 2 11
22 a A 2 22
23 a A 3 11
24 a A 3 22
25 b B 1 11
26 b B 1 22
27 b B 2 11
28 b B 2 22
29 b B 3 11
30 b B 3 22
31 c C 1 11
32 c C 1 22
33 c C 2 11
34 c C 2 22
35 c C 3 11
36 c C 3 22