# A Guide to Permutations in R

permutation is an arrangement of objects where the order is important.

The number of permutations of objects taken at a time, denoted as nPcan be found using the formula:

nPr  =  n!  / (n-r)!

For example, suppose we want to choose three people from a group of six to fill three positions on a council. The number of ways three people can be chosen from a group of six is:

6P3 = 6! / (6-3)!  =  (6*5*4*3*2*1) / (3*2*1)  = 720 / 6 = 120

This tutorial explains how to work with permutations in R.

## Finding Total Permutations in R

To find the number of permutations of objects taken at a time, we can write a simple function in R:

```#create function to determine number of permutations of n objects taken r at a time
permutations <- function(n, r){
return(factorial(n)/factorial(n - r))
}
```

Example 1: Suppose a club of six people wants to choose a council of three officers: a president, a vice president, and a treasury. How many ways are there to choose the council from the six people?

```permutations(6, 3)

# [1] 120```

There are 120 different ways to choose the council of three people from size people.

Example 2: A group of 15 students are trying out for a musical that only has 4 roles . How many different ways are there to choose 4 roles from these 15 students?

```permutations(15, 4)

# [1] 32760```

There are 32,760 different ways to choose 4 roles from these 15 students.

Example 3: Jessica has seven plants in her garden. She must choose three of the plants, one to put in the front yard, one to put in the side yard, and one to put in the back yard. How many different ways are there for her to choose three plants from the seven plants?

```permutations(7, 3)

# [1] 210```

There are 210 different ways for Jessica to choose three plants from the seven plants.

## Listing Out Permutations in R

To list out all of the actual different permutations for a given problem, we can use the permn function from the combinat library.

```#load combinat library
library(combinat)

#list out all possible permutations of the list "A", "B", "C"
permn(c("A", "B", "C"))
[[1]]
[1] "A" "B" "C"

[[2]]
[1] "A" "C" "B"

[[3]]
[1] "C" "A" "B"

[[4]]
[1] "C" "B" "A"

[[5]]
[1] "B" "C" "A"

[[6]]
[1] "B" "A" "C"

#list out all possible permutations of the list "Bob", "Mike", "Susan", "Rachel"
permn(c("Bob", "Mike", "Susan", "Rachel"))
[[1]]
[1] "Bob"    "Mike"   "Susan"  "Rachel"

[[2]]
[1] "Bob"    "Mike"   "Rachel" "Susan"

[[3]]
[1] "Bob"    "Rachel" "Mike"   "Susan"

[[4]]
[1] "Rachel" "Bob"    "Mike"   "Susan"

[[5]]
[1] "Rachel" "Bob"    "Susan"  "Mike"

[[6]]
[1] "Bob"    "Rachel" "Susan"  "Mike"

[[7]]
[1] "Bob"    "Susan"  "Rachel" "Mike"

[[8]]
[1] "Bob"    "Susan"  "Mike"   "Rachel"

[[9]]
[1] "Susan"  "Bob"    "Mike"   "Rachel"

[[10]]
[1] "Susan"  "Bob"    "Rachel" "Mike"

[[11]]
[1] "Susan"  "Rachel" "Bob"    "Mike"

[[12]]
[1] "Rachel" "Susan"  "Bob"    "Mike"

[[13]]
[1] "Rachel" "Susan"  "Mike"   "Bob"

[[14]]
[1] "Susan"  "Rachel" "Mike"   "Bob"

[[15]]
[1] "Susan"  "Mike"   "Rachel" "Bob"

[[16]]
[1] "Susan"  "Mike"   "Bob"    "Rachel"

[[17]]
[1] "Mike"   "Susan"  "Bob"    "Rachel"

[[18]]
[1] "Mike"   "Susan"  "Rachel" "Bob"

[[19]]
[1] "Mike"   "Rachel" "Susan"  "Bob"

[[20]]
[1] "Rachel" "Mike"   "Susan"  "Bob"

[[21]]
[1] "Rachel" "Mike"   "Bob"    "Susan"

[[22]]
[1] "Mike"   "Rachel" "Bob"    "Susan"

[[23]]
[1] "Mike"   "Bob"    "Rachel" "Susan"

[[24]]
[1] "Mike"   "Bob"    "Susan"  "Rachel"
```

Note that if your list is longer than 4, the number of possible permutations begins to increase dramatically.