Combinations

You can calculate how many ways can k items be taken from n items by using the following command.

choose(n=5,k=2);

Load the file example.csv See Loading Data for help.

example.data <- read.csv(file="http://homepages.gac.edu/~anienow2/MCS_142/R/example.csv", header=TRUE, sep=",")

Generate all possible combinations of size 2 from a list.

my.list <- example.data$Name # creates a list of names
combn(my.list, 2); # Produces all possible combinations of size = 2 from my.list.
##      [,1]   [,2]   [,3]   [,4]   [,5]  [,6]  [,7]  [,8]  [,9]  [,10]
## [1,] Shelly Shelly Shelly Shelly Alvin Alvin Alvin Simon Simon Theo 
## [2,] Alvin  Simon  Theo   Nicki  Simon Theo  Nicki Theo  Nicki Nicki
## Levels: Alvin Nicki Shelly Simon Theo

Each column is one combination. NOTE, this is not a randomly generated list. The sample() function should be used for generating random samples.

Permutations

To calculate the number of possible permutations for a list of n distinct items use the following command. In this case, n = 5.

factorial(x=5);
## [1] 120

Mathematicss, Computer Science, and Statistics Department Gustavus Adolphus College