Reputation: 11
I have a vector of the first 20 letters of the alphabet, and I want to create a dataframe of 6 columns that contains every combination of 6 letters.
I've generated every possible permutation, and using the subset function I am able to whittle down my total number of observations to eliminate duplicates, but the code I have is horribly inefficient. Is there a way to go straight for the kill and generate the combinations rather than starting with permutations and going from there?
See code below:
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))), ])
The idea behind the subsets is to cut down on the number of observations before executing the combinations code to save computing time, but I have a feeling that there's a better way to go about doing this
Upvotes: 0
Views: 73
Reputation: 24148
Sounds like you need combn
from utils
:
?combn
Generate all combinations of the elements of x taken m at a time.
a <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t")
combinations <- t(combn(a, 6))
combinations <- data.frame(combinations)
Upvotes: 1