在R中展开未知尺寸的网格 [英] Expand grid with unknown dimension in R

查看:107
本文介绍了在R中展开未知尺寸的网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For a given vector x, I need to obtain quantities of the type

expand.grid(x,x,x,x)

其中x重复d次。有没有功能允许这个?像

where x is repeated d times. Is there a function that allows this? Something like

expand.grids(x,d)

谢谢!

推荐答案

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

这篇关于在R中展开未知尺寸的网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆