在 R 中使用 expand.grid 创建 y 集合中 x 因子的所有可能组合

Use expand.grid in R to create all possible combinations of x factors in sets of y

是否可以在 R 中使用 expand.grid() 来创建 y 集合中 x 因子的所有可能组合?

比如我有12个因素:

Factor1 = c("1", "2", "3", "4"),       #Fixed Attribute: 4 lvls
Factor2 = c("5", "6", "7", "8", "9"),  #Fixed Attribute: 5 lvls
Factor3 = c("10", "11", "12","13"),    #Fixed Attribute: 4 lvls
Factor4 = c("14", "15", "16"),         #Fixed Attribute: 4 lvls
Factor5 = c("17", "18", "19", "20", "21"),  #Variable Attribute: 5 lvls
Factor6 = c("22", "23"),                    #Variable Attribute: 2 lvls
Factor7 = c("24", "25", "26"),              #Variable Attribute: 3 lvls
Factor8 = c("27", "28", "29")               #Variable Attribute: 3 lvls
Factor9 = c("30", "31", "32", "33"),        #Variable Attribute: 4 lvls
Factor10= c("34", "35"),                    #Variable Attribute: 2 lvls
Factor11 = c("36", "37", "38"),             #Variable Attribute: 3 lvls
Factor12 = c("39", "40", "41")              #Variable Attribute: 3 lvls

我想始终在 expand.grid() 中包括前 4 个(即它们是固定的),并在所有可能的 4 组中循环最后 8 个,这等于 70 个独特的组。然后追加所有产生的 70 个数据帧。

我可以通过创建 70 个不同的 expand.grid() 代码块来使用蛮力方法,但是有没有技术上不太优雅的方法来做到这一点?

例如暴力破解方式如下:

expand.grid(Factor1, Factor2,Factor3,Factor4,Factor5,Factor6,Factor7,Factor8)
expand.grid(Factor1, Factor2,Factor3,Factor4,Factor5,Factor6,Factor7,Factor9)
expand.grid(Factor1, Factor2,Factor3,Factor4,Factor5,Factor6,Factor7,Factor10)
expand.grid(Factor1, Factor2,Factor3,Factor4,Factor5,Factor6,Factor7,Factor11)
expand.grid(Factor1, Factor2,Factor3,Factor4,Factor5,Factor6,Factor7,Factor12)
....etc...

所以我最终会得到 70 个不同的数据框,因为有 70 种独特的方法 select 4-12 中的 4 个因素(即 70 种方法 select 列表中的 4 个项目8)

此外,我生成的数据框可能有 150 万行。这会导致内存问题吗?

谢谢,

如果我没理解错的话,这应该可以满足您的要求:

l <- list(
    Factor1 = c("1", "2", "3", "4"),       #Fixed Attribute: 4 lvls
    Factor2 = c("5", "6", "7", "8", "9"),  #Fixed Attribute: 5 lvls
    Factor3 = c("10", "11", "12","13"),    #Fixed Attribute: 4 lvls
    Factor4 = c("14", "15", "16"),         #Fixed Attribute: 4 lvls
    Factor5 = c("17", "18", "19", "20", "21"),  #Variable Attribute: 5 lvls
    Factor6 = c("22", "23"),                    #Variable Attribute: 2 lvls
    Factor7 = c("24", "25", "26"),              #Variable Attribute: 3 lvls
    Factor8 = c("27", "28", "29"),               #Variable Attribute: 3 lvls,
    Factor9 = c("30", "31", "32", "33"),        #Variable Attribute: 4 lvls
    Factor10= c("34", "35"),                    #Variable Attribute: 2 lvls
    Factor11 = c("36", "37", "38"),             #Variable Attribute: 3 lvls
    Factor12 = c("39", "40", "41")              #Variable Attribute: 3 lvls
)



# Get the names of the other 8
others <- names(l)[-c(1:4)]
# Get names of the 4 fixed ones
fixed <- names(l)[1:4]

# Get all combinations of 4 of names of the others
combos <- combn(others, 4)

# Get the list of 70 expand grid outputs of combinations (fixed, combo_of_4)
out <- apply(combos, 2, function(x) expand.grid(l[c(fixed,x)]))