Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R generate all possible combinations of size m from of a character vector of n elements [duplicate]

Tags:

r

combinations

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?

like image 436
Vahid Mirjalili Avatar asked Oct 22 '25 20:10

Vahid Mirjalili


2 Answers

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

do.call(expand.grid, rep(list(x), 3))
like image 70
Roland Avatar answered Oct 25 '25 10:10

Roland


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" 
…
like image 45
hrbrmstr Avatar answered Oct 25 '25 10:10

hrbrmstr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!