B. Rowe B. Rowe - 1 month ago 15
R Question

R Programming- Permutation regarding repetition and order

I have attempted to search and attempt solutions to no avail with the combn and gtools library.

I want to take a vector of the following:

x<-c(TRUE,FALSE)


and have it look like the following output:

Permutations with repetition (n=2, r=5)
Using Items: t,f

List has 32 entries.
{t,t,t,t,t} {t,t,t,t,f} {t,t,t,f,t} {t,t,t,f,f} {t,t,f,t,t} {t,t,f,t,f} {t,t,f,f,t} {t,t,f,f,f} {t,f,t,t,t} {t,f,t,t,f} {t,f,t,f,t} {t,f,t,f,f} {t,f,f,t,t} {t,f,f,t,f} {t,f,f,f,t} {t,f,f,f,f} {f,t,t,t,t} {f,t,t,t,f} {f,t,t,f,t} {f,t,t,f,f} {f,t,f,t,t} {f,t,f,t,f} {f,t,f,f,t} {f,t,f,f,f} {f,f,t,t,t} {f,f,t,t,f} {f,f,t,f,t} {f,f,t,f,f} {f,f,f,t,t} {f,f,f,t,f} {f,f,f,f,t} {f,f,f,f,f}


Any suggestions? I am quite a newbie at this, so any help is appreciated. I used the following online calculator to give me the solution below. https://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html

Thanks!

Answer

Using the gtools library, I believe this is:

library(gtools)
permutations(2,5,v=c(TRUE,FALSE),repeats.allowed=TRUE)
##       [,1]  [,2]  [,3]  [,4]  [,5]
## [1,] FALSE FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE  TRUE
## [3,] FALSE FALSE FALSE  TRUE FALSE
## [4,] FALSE FALSE FALSE  TRUE  TRUE
## [5,] FALSE FALSE  TRUE FALSE FALSE
## [6,] FALSE FALSE  TRUE FALSE  TRUE
## [7,] FALSE FALSE  TRUE  TRUE FALSE
## [8,] FALSE FALSE  TRUE  TRUE  TRUE
## [9,] FALSE  TRUE FALSE FALSE FALSE
##[10,] FALSE  TRUE FALSE FALSE  TRUE
##[11,] FALSE  TRUE FALSE  TRUE FALSE
##[12,] FALSE  TRUE FALSE  TRUE  TRUE
##[13,] FALSE  TRUE  TRUE FALSE FALSE
##[14,] FALSE  TRUE  TRUE FALSE  TRUE
##[15,] FALSE  TRUE  TRUE  TRUE FALSE
##[16,] FALSE  TRUE  TRUE  TRUE  TRUE
##[17,]  TRUE FALSE FALSE FALSE FALSE
##[18,]  TRUE FALSE FALSE FALSE  TRUE
##[19,]  TRUE FALSE FALSE  TRUE FALSE
##[20,]  TRUE FALSE FALSE  TRUE  TRUE
##[21,]  TRUE FALSE  TRUE FALSE FALSE
##[22,]  TRUE FALSE  TRUE FALSE  TRUE
##[23,]  TRUE FALSE  TRUE  TRUE FALSE
##[24,]  TRUE FALSE  TRUE  TRUE  TRUE
##[25,]  TRUE  TRUE FALSE FALSE FALSE
##[26,]  TRUE  TRUE FALSE FALSE  TRUE
##[27,]  TRUE  TRUE FALSE  TRUE FALSE
##[28,]  TRUE  TRUE FALSE  TRUE  TRUE
##[29,]  TRUE  TRUE  TRUE FALSE FALSE
##[30,]  TRUE  TRUE  TRUE FALSE  TRUE
##[31,]  TRUE  TRUE  TRUE  TRUE FALSE
##[32,]  TRUE  TRUE  TRUE  TRUE  TRUE