How do i find all possible permutations of the first 1-2 letters of each word?

0

Issue

I have the following data:

inputs <- c("Master", "Bachelor", "School")

I would like to have all possible permutations of the first 1-2 letters of each
word.

first_letter <- sapply(inputs, substr, start = 1, stop = 1)
"M" "B"  "S"

second_letter <- sapply(inputs, substr, start = 1, stop = 2)
"Ma"     "Ba"     "Sc" 

Desired output:

All permutations of the first letters in every order, see the columns of
variable "all_order" (see section "What i tried").
Also in both variations, so either take the first value of
"first_letter" or first value "second_letter" but not both at the same time.

MBaS, MBS, MBSc, MBaSc MaBaS, MaBS, MaBSc, MaBaSc,

SBM, SBaM, SBaMa, SBaM ScBM, ScBaM, ScBaMa, ScBaM

BSM, BSMa, BScM, BScMa BaSM, BaSMa, BaScM, BaScMa,

…..

(Let me know if it is explained well enough.)

What i tried:

combs <- combn(rep(seq(inputs), 2), 3)
keep <- !colSums(apply(combs, 2, duplicated))
all_order <- combs[, keep]

all_order
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    1    1    1    2    2    3    1
[2,]    2    2    3    2    3    1    1    2
[3,]    3    3    2    3    1    3    2    3

Solution

We’ll use gtools::permutations to calculate the … permutations of inputs, and then use expand.grid to show all combinations within them.

First, we can do it easily on one order of the inputs with:

expand.grid(c("M","Ma"), c("B","Ba"), c("S","Sc"))
#   Var1 Var2 Var3
# 1    M    B    S
# 2   Ma    B    S
# 3    M   Ba    S
# 4   Ma   Ba    S
# 5    M    B   Sc
# 6   Ma    B   Sc
# 7    M   Ba   Sc
# 8   Ma   Ba   Sc
do.call(paste, c(expand.grid(c("M","Ma"), c("B","Ba"), c("S","Sc")), sep = ""))
# [1] "MBS"    "MaBS"   "MBaS"   "MaBaS"  "MBSc"   "MaBSc"  "MBaSc"  "MaBaSc"

Now that’s one order (M < B < S), now we need to rearrange them. We might call all orderings manually, or we can use gtools::permutations to help.

inputlist <- lapply(inputs, substring, 1, 1:2)
str(inputlist)
# List of 3
#  $ : chr [1:2] "M" "Ma"
#  $ : chr [1:2] "B" "Ba"
#  $ : chr [1:2] "S" "Sc"

perms <- gtools::permutations(3, 3)
perms
#      [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    1    3    2
# [3,]    2    1    3
# [4,]    2    3    1
# [5,]    3    1    2
# [6,]    3    2    1
inputlist[perms[2,]]
# [[1]]
# [1] "M"  "Ma"
# [[2]]
# [1] "S"  "Sc"
# [[3]]
# [1] "B"  "Ba"
inputlist[perms[3,]]
# [[1]]
# [1] "B"  "Ba"
# [[2]]
# [1] "M"  "Ma"
# [[3]]
# [1] "S"  "Sc"

Resulting in

allperms <- do.call(rbind,
  apply(gtools::permutations(3, 3), 1, 
        function(ind) do.call(expand.grid, inputlist[ind]))
)
head(allperms); tail(allperms)
#   Var1 Var2 Var3
# 1    M    B    S
# 2   Ma    B    S
# 3    M   Ba    S
# 4   Ma   Ba    S
# 5    M    B   Sc
# 6   Ma    B   Sc
#    Var1 Var2 Var3
# 43    S   Ba    M
# 44   Sc   Ba    M
# 45    S    B   Ma
# 46   Sc    B   Ma
# 47    S   Ba   Ma
# 48   Sc   Ba   Ma
do.call(paste, c(allperms, list(sep = "")))
#  [1] "MBS"    "MaBS"   "MBaS"   "MaBaS"  "MBSc"   "MaBSc"  "MBaSc"  "MaBaSc" "MSB"    "MaSB"   "MScB"  
# [12] "MaScB"  "MSBa"   "MaSBa"  "MScBa"  "MaScBa" "BMS"    "BaMS"   "BMaS"   "BaMaS"  "BMSc"   "BaMSc" 
# [23] "BMaSc"  "BaMaSc" "BSM"    "BaSM"   "BScM"   "BaScM"  "BSMa"   "BaSMa"  "BScMa"  "BaScMa" "SMB"   
# [34] "ScMB"   "SMaB"   "ScMaB"  "SMBa"   "ScMBa"  "SMaBa"  "ScMaBa" "SBM"    "ScBM"   "SBaM"   "ScBaM" 
# [45] "SBMa"   "ScBMa"  "SBaMa"  "ScBaMa"

Answered By – r2evans

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More