0

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

  • `as.vector(a[[1]])` undoes what `data.frame(a)` did. – Rui Barradas Jan 07 '23 at 06:49
  • Does this answer your question? [How to generate permutations or combinations of object in R?](https://stackoverflow.com/questions/22569176/how-to-generate-permutations-or-combinations-of-object-in-r) – Joseph Wood Jan 07 '23 at 23:14
  • There are other options as well on stackoverflow: E.g. https://stackoverflow.com/q/26828301/4408538 https://stackoverflow.com/q/49563565/4408538 – Joseph Wood Jan 07 '23 at 23:20

1 Answers1

1

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)
m0nhawk
  • 22,980
  • 9
  • 45
  • 73