0

The simplest way to generate such combinations would be to use combn function as follows:

print(combn(letters[1:3],2))

The output that it generates is:

    [,1] [,2] [,3]
[1,] "a"  "a"  "b" 
[2,] "b"  "c"  "c" 

This doesn't generate combinations like aa, bb (same character repeated). This even doesn't generate ba if ab is generated.

I want to generate all such combinations of length 2 for a vector [a,b,c].

Is there a simple way to do so in R?

Sohan Shirodkar
  • 510
  • 3
  • 24

1 Answers1

2

The paste0 function is vectorized and so succeeds with outer:

outer(c('a','b','c'), c('a','b','c'), paste0)

     [,1] [,2] [,3]
[1,] "aa" "ab" "ac"
[2,] "ba" "bb" "bc"
[3,] "ca" "cb" "cc"
IRTFM
  • 258,963
  • 21
  • 364
  • 487