Enumeration of combinations and permutations in R

This post demonstrates how to enumerate combinations and permutations using R.



Enumeration of combinations



The enumeration of combinations and permutations in R is simple. We can use the combinations() and permutations() functions from gtools R library.

Simple example is as follows.

library(gtools)
 
# combination and permutation with or without replacement
combinations(3,2,letters[1:3])
combinations(3,2,letters[1:3],repeats=TRUE)
 
permutations(3,2,letters[1:3])
permutations(3,2,letters[1:3],repeats=TRUE)
 
cs


> # combination and permutation with or without replacement
> combinations(3,2,letters[1:3])
     [,1] [,2]
[1,] "a"  "b" 
[2,] "a"  "c" 
[3,] "b"  "c" 
> combinations(3,2,letters[1:3],repeats=TRUE)
     [,1] [,2]
[1,] "a"  "a" 
[2,] "a"  "b" 
[3,] "a"  "c" 
[4,] "b"  "b" 
[5,] "b"  "c" 
[6,] "c"  "c" 
> 
> permutations(3,2,letters[1:3])
     [,1] [,2]
[1,] "a"  "b" 
[2,] "a"  "c" 
[3,] "b"  "a" 
[4,] "b"  "c" 
[5,] "c"  "a" 
[6,] "c"  "b" 
> permutations(3,2,letters[1:3],repeats=TRUE)
      [,1] [,2]
 [1,] "a"  "a" 
 [2,] "a"  "b" 
 [3,] "a"  "c" 
 [4,] "b"  "a" 
 [5,] "b"  "b" 
 [6,] "b"  "c" 
 [7,] "c"  "a" 
 [8,] "c"  "b" 
 [9,] "c"  "c" 
> 
cs



In the next example, I utilize combinations with 3 factors and 3 segments to achieve some intended outcomes.

# combinations of 3 segments and 3 factors
vf <- c("L""S""C"# 3 factors
vs <- 1:3              # 3 segments
 
vfs <- as.vector(outer(vf, vs, paste0))
vfs
 
# combinations
combinations(6,2,vfs[4:9]) # as column names
combinations(6,2,4:9# as column numbers
 
cs


> # combinations of 3 segments and 3 factors
> vf <- c("L""S""C"# 3 factors
> vs <- 1:3              # 3 segments
> 
> vfs <- as.vector(outer(vf, vs, paste0))
> vfs
[1"L1" "S1" "C1" "L2" "S2" "C2" "L3" "S3" "C3"
> 
> # combinations
> combinations(6,2,vfs[4:9]) # as column names
      [,1] [,2]
 [1,] "C2" "C3"
 [2,] "C2" "L2"
 [3,] "C2" "L3"
 [4,] "C2" "S2"
 [5,] "C2" "S3"
 [6,] "C3" "L2"
 [7,] "C3" "L3"
 [8,] "C3" "S2"
 [9,] "C3" "S3"
[10,] "L2" "L3"
[11,] "L2" "S2"
[12,] "L2" "S3"
[13,] "L3" "S2"
[14,] "L3" "S3"
[15,] "S2" "S3"
> combinations(6,2,4:9# as column numbers
      [,1] [,2]
 [1,]    4    5
 [2,]    4    6
 [3,]    4    7
 [4,]    4    8
 [5,]    4    9
 [6,]    5    6
 [7,]    5    7
 [8,]    5    8
 [9,]    5    9
[10,]    6    7
[11,]    6    8
[12,]    6    9
[13,]    7    8
[14,]    7    9
[15,]    8    9
> 
cs



Finally, I demonstrate how to combine combinations from multiple groups, such as drawing one from group A and two from group B.

# combinations from each segment
c1 <- combinations(3,1,1:3)
c2 <- combinations(6,2,4:9)
 
c1
c2
 
no_eg <- expand.grid(1:nrow(c1), 1:nrow(c2))
co_eg <- cbind(c1[no_eg[,1],], c2[no_eg[,2],])
co_eg
 
cs


> # combinations from each segment
> c1 <- combinations(3,1,1:3)
> c2 <- combinations(6,2,4:9)
> 
> c1
     [,1]
[1,]    1
[2,]    2
[3,]    3
> c2
      [,1] [,2]
 [1,]    4    5
 [2,]    4    6
 [3,]    4    7
          ...
[13,]    7    8
[14,]    7    9
[15,]    8    9
> 
> no_eg <- expand.grid(1:nrow(c1), 1:nrow(c2))
> co_eg <- cbind(c1[no_eg[,1],], c2[no_eg[,2],])
> co_eg
      [,1] [,2] [,3]
 [1,]    1    4    5
 [2,]    2    4    5
 [3,]    3    4    5
 [4,]    1    4    6
          ...
[42,]    3    7    9
[43,]    1    8    9
[44,]    2    8    9
[45,]    3    8    9
> 
cs



No comments:

Post a Comment