I am trying to get all the possible combinations of length 3 of the elements of a variable. Although it partly worked with combn() I did not quite get the output I was looking for. Here's my example
x <- c("a","b","c","d","e")
t(combn(c(x,x), 3))
The output I get looks like this
[,1] [,2] [,3]
[1,] "a" "b" "c"
[2,] "a" "b" "d"
[3,] "a" "b" "e"
I am not really happy with this command for 2 reasons. I wanted to get an output that says "a+b+c" "a+b+b"...., unfortunately I wasn't able to edit the output with paste() or something.
I was also looking forward for one combination of each set of letters, that is I either get "a+b+c" or "b+a+c" but not both.
combn
takes aFUN
argument:t(combn(c(x,x), 3, paste0, collapse = "+"))
– Capetiancombn(x,3)
) or permutations(expand.grid(x,x,x)
). Not sure why you're duplicating x in your combn arguement. – Souterletters[1:5]
would be useful to you – Ojeda