Combination without repetition in R
Asked Answered
L

2

8

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.

Lamblike answered 24/1, 2014 at 17:53 Comment(3)
For the first part of the question, note that combn takes a FUN argument: t(combn(c(x,x), 3, paste0, collapse = "+"))Capetian
I can't tell from your description if you want combinations (combn(x,3)) or permutations( expand.grid(x,x,x)). Not sure why you're duplicating x in your combn arguement.Souter
also the function letters[1:5] would be useful to youOjeda
S
9

Try something like:

x <- c("a","b","c","d","e")
d1 <- combn(x,3) # All combinations

d1 

#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] "a"  "a"  "a"  "a"  "a"  "a"  "b"  "b"  "b"  "c"  
# [2,] "b"  "b"  "b"  "c"  "c"  "d"  "c"  "c"  "d"  "d"  
# [3,] "c"  "d"  "e"  "d"  "e"  "e"  "d"  "e"  "e"  "e"

nrow(unique(t(d1))) == nrow(t(d1))
# [1] TRUE

d2 <- expand.grid(x,x,x) # All permutations 

d2

#     Var1 Var2 Var3
# 1      a    a    a
# 2      b    a    a
# 3      c    a    a
# 4      d    a    a
# 5      e    a    a
# 6      a    b    a
# 7      b    b    a
# 8      c    b    a
# 9      d    b    a
# ...

nrow(unique(d2)) == nrow(d2)
# [1] TRUE
Souter answered 24/1, 2014 at 17:59 Comment(9)
this gives duplicates which is not what the OP wantsOjeda
no, I was just responding without thinking to what I thought @Ojeda was saying. Sorry, will delete.Taub
@Ojeda Which gives duplicates? (PS I like your name)Souter
I guess it's really not clear what OP is looking for. The use of combn(x, 3) and combn(c(x,x), 3) and why OP would want "a + b + c" and NOT "a + c + b" while something like "a + a + a" is acceptable.Ojeda
You know, now that I read what he wants again, I'm thinking he may literally want "a+b+c". I just thought he wanted the combinations, but he may want that actual text.Souter
Thanks for your kind responses everyone. I am actually looking for a results with no repetitions at all, so even a "a+b+b" should be troublesome for me. As @BrandonBertelsen has mentioned, I am looking forward to get all the combinations of the text, so if they were like "var1", "var2" .... i would like to get something like "var1+var2+var3" without having "var1+var1+var2" because this is a repetition. Thanks again for your help guys.Lamblike
The command I was looking for was combn(x, 3, paste, collapse = '+'). Cheers everyoneLamblike
I deleted that in the comments hahaOjeda
I thought it was you too.Souter
H
2

try this

x <- c("a","b","c","d","e")
expand.grid(rep(list(x), 3))
Hortatory answered 20/5, 2020 at 0:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.