我有一个由字母表的前20个字母组成的向量,我想创建一个包含6列的 Dataframe ,其中包含6个字母的所有组合。
我已经生成了每一种可能的排列,使用子集函数,我可以减少观测的总数,以消除重复,但我的代码效率非常低,有没有办法直接杀死并生成组合,而不是从排列开始,然后从那里开始?
参见以下代码:
a = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t")
a = data.frame(a)
a = as.vector(a[[1]])
permutations = data.frame((expand.grid(a,a,a,a,a,a,
stringsAsFactors = FALSE)))
permutations = subset(permutations, Var1!=Var2)
permutations = subset(permutations, Var1!=Var3)
permutations = subset(permutations, Var1!=Var4)
permutations = subset(permutations, Var1!=Var5)
permutations = subset(permutations, Var1!=Var6)
permutations = subset(permutations, Var2!=Var3)
permutations = subset(permutations, Var2!=Var4)
permutations = subset(permutations, Var2!=Var5)
permutations = subset(permutations, Var2!=Var6)
permutations = subset(permutations, Var3!=Var4)
permutations = subset(permutations, Var3!=Var5)
permutations = subset(permutations, Var3!=Var6)
permutations = subset(permutations, Var4!=Var5)
permutations = subset(permutations, Var4!=Var6)
permutations = subset(permutations, Var5!=Var6)
combinations = data.frame(permutations[!duplicated(t(apply(
permutations[c("Var1",
"Var2",
"Var3",
"Var4",
"Var5",
"Var6")], 1, sort))), ])
子集背后的想法是在执行组合代码之前减少观察的数量,以保存计算时间,但我有一种感觉,有一种更好的方法来实现这一点
1条答案
按热度按时间xqkwcwgp1#
听起来您需要从
utils
获得combn
:生成x中每次取m的元素的所有组合。