Combinatorics

TODO

  • link to sets

Install required packages

e1071, permute

Combinations

Number of \(k\)-combinations

\({n \choose k}\)

[1] 5
[1] 5

Enumerate all combinations

     [,1] [,2] [,3] [,4] [,5]
[1,] "a"  "a"  "a"  "a"  "b" 
[2,] "b"  "b"  "b"  "c"  "c" 
[3,] "c"  "c"  "d"  "d"  "d" 
[4,] "d"  "e"  "e"  "e"  "e" 
     [,1] [,2] [,3] [,4]
[1,]    1    1    1    2
[2,]    2    2    3    3
[3,]    3    4    4    4
[1] 6 7 8 9
[1] 1.8 2.1 2.3 2.8

Permutations

Number of permutations

[1] 5040

Random permutation

 [1] "C" "J" "B" "H" "F" "I" "A" "G" "E" "D"
 [1] 10  5  3  8  1  4  6  9  7  2

Enumerate all permutations

All permutations at once

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    1    3
[3,]    2    3    1
[4,]    1    3    2
[5,]    3    1    2
[6,]    3    2    1
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "A"  "B"  "B"  "A"  "C"  "C" 
[2,] "B"  "A"  "C"  "C"  "A"  "B" 
[3,] "C"  "C"  "A"  "B"  "B"  "A" 

Each permutation individually

[1] "a" "a" "a" "b" "b" "b" "c" "c" "c"
[1] "c" "a" "c" "c" "a" "b" "a" "b" "b"
[1] "a" "a" "c" "b" "b" "c" "a" "c" "b"
[1] "c" "a" "a" "a" "b" "c" "b" "b" "c"
[1] "b" "a" "a" "b" "c" "b" "a" "c" "c"
[1] "a" "c" "a" "c" "b" "c" "b" "b" "a"

Restricted permutations

Permute group membership in a two-way ANOVA design. Here: artificially low number of permutations.

   id IV1 IV2
1   1   1   1
2   2   1   2
3   3   1   3
4   4   1   1
5   5   1   2
6   6   1   3
7   7   1   1
8   8   1   2
9   9   1   3
10 10   1   1
11 11   1   2
12 12   1   3
13 13   2   1
14 14   2   2
15 15   2   3
16 16   2   1
17 17   2   2
18 18   2   3
19 19   2   1
20 20   2   2
21 21   2   3
22 22   2   1
23 23   2   2
24 24   2   3

Get permutations for test of factor A

Get permutations for test of factor B

Enumerate all \(n\)-tuples

All combinations of elements from \(n\) different sets (cartesian product)

       Var1 Var2 Var3
1   control    f    1
2 treatment    f    1
3   control    m    1
4 treatment    m    1
5   control    f    2
6 treatment    f    2
7   control    m    2
8 treatment    m    2

Apply a function to all pairs of elements from two sets

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    2    4    6    8   10
[3,]    3    6    9   12   15
[4,]    4    8   12   16   20
[5,]    5   10   15   20   25

Detach (automatically) loaded packages (if possible)

Get the article source from GitHub

R markdown - markdown - R code - all posts