John Gagnon
John Gagnon

Reputation: 915

How to generate all permutations of a combination of two vectors based on a specified sequence?

I am trying to create a list of all possible permutations of the combination of values from S with values from G based on a specific number of repetitions. For example 4 repetitions would look like: SGSGSGSG

S <- c("TCT", "TCC", "TCG", "TCA", "AGT", "AGC")
G <- c("GGT", "GGC", "GGA", "GGG")

Using the values from the vectors, some example combination would be:

"TCTGGTTCTGGTTCTGGTTCTGGT"
"TCTGGTTCCGGCTCGGGATCAGGG"

I have found that I can make permutations of each vector independently with the gtools package but this is not really what I'm looking for:

pS <- gtools::permutations(v = S, n = length(S), r = 4, repeats.allowed = TRUE)
pG <- gtools::permutations(v = G, n = length(G), r = 4, repeats.allowed = TRUE)

Is there a fast way to make all permutations the way I described without the use of a for loop?

I have looked at 'expand.grid()', however, I don't know of a way to restrict this function to giving me only the permutations that consist of SGSGSGSG...

Upvotes: 1

Views: 139

Answers (1)

akrun
akrun

Reputation: 887183

It may be more efficient to use permuteGeneral

library(RcppAlgos)
permuteGeneral(S, m = length(S), freqs = rep(4, length(S)))

Upvotes: 1

Related Questions