Example R programs and commands
4. Combinations and permutations
# All lines preceded by the "#" character are my comments.
# All other left-justified lines are my input.
# All other indented lines are the R program output.
# Count the number of ways to choose k distinct objects from a list of n:
k<-3; n<-5; choose(n,k)
[1] 10
# Generate all the possible choices as column vectors:
combn(n,k)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 1 1 1 2 2 2 3
[2,] 2 2 2 3 3 4 3 3 4 4
[3,] 3 4 5 4 5 5 4 5 5 5