在R中展开具有未知维度的网格

spl*_*123 1 r data-structures

对于给定的向量x,我需要获得该类型的数量

expand.grid(x,x,x,x)
Run Code Online (Sandbox Code Playgroud)

其中x重复d次.有没有允许这个的功能?就像是

expand.grids(x,d)
Run Code Online (Sandbox Code Playgroud)

谢谢!

Jos*_*ien 5

expand.grids <- function(x,d) {
    expand.grid(replicate(d, x, simplify=FALSE))
}

expand.grids(1:2,4)
   Var1 Var2 Var3 Var4
1     1    1    1    1
2     2    1    1    1
3     1    2    1    1
4     2    2    1    1
5     1    1    2    1
6     2    1    2    1
7     1    2    2    1
8     2    2    2    1
9     1    1    1    2
10    2    1    1    2
11    1    2    1    2
12    2    2    1    2
13    1    1    2    2
14    2    1    2    2
15    1    2    2    2
16    2    2    2    2
Run Code Online (Sandbox Code Playgroud)