4

so, I have this vector c("T", "A", "C", "G") for genomic data. I want to generate all possible combinations of size 3, with repeats such as:

T T T
T T A
T T C
T T G
T A T
..

that would give me 4^3=64 combinations. Combinations of size 4 would give 4^4, and for size 5 should give 4^5=1024 rows.

I searched through SOF, and think expand.grid() would do that, but I couldn't find out how to use it to get the desired output. Any idea?

Vahid Mirjalili
  • 6,211
  • 15
  • 57
  • 80

2 Answers2

5
x <- c("T", "A", "C", "G")

do.call(expand.grid, rep(list(x), 3))
Roland
  • 127,288
  • 10
  • 191
  • 288
5

permutations from gtools is designed to do just this:

library(gtools)

data <-  c("T", "A", "C", "G")

permutations(4, 3, data, repeats.allowed = TRUE)
##       [,1] [,2] [,3]
##  [1,] "A"  "A"  "A" 
##  [2,] "A"  "A"  "C" 
##  [3,] "A"  "A"  "G" 
##  [4,] "A"  "A"  "T" 
##  [5,] "A"  "C"  "A" 
##  [6,] "A"  "C"  "C" 
##  [7,] "A"  "C"  "G" 
##  [8,] "A"  "C"  "T" 
##  [9,] "A"  "G"  "A" 
## [10,] "A"  "G"  "C" 
## [11,] "A"  "G"  "G" 
## [12,] "A"  "G"  "T" 
## [13,] "A"  "T"  "A" 
## [14,] "A"  "T"  "C" 
## [15,] "A"  "T"  "G" 
## [16,] "A"  "T"  "T" 
## [17,] "C"  "A"  "A" 
## [18,] "C"  "A"  "C" 
## [19,] "C"  "A"  "G" 
## [20,] "C"  "A"  "T" 
…
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205