# Calculate Combinations & Permutations in R (4 Examples)

In this R tutorial you’ll learn how to **generate and count all possible permutations and combinations of the elements in a vector**.

The tutorial will contain the following information:

Before we jump into the examples, we need to install and load the combinat package:

install.packages("combinat") # Install combinat package library("combinat") # Load combinat package |

install.packages("combinat") # Install combinat package library("combinat") # Load combinat package

So now the part you have been waiting for – the exemplifying R syntax.

## Example 1: Create List Containing All Possible Permutations

The following R programming code shows how to generate a list of all possible permutations in R.

For this, we can apply the permn function that is provided by the combinat package:

permut <- permn(3) # Create list of permutations permut # Print list of permutations # [[1]] # [1] 1 2 3 # # [[2]] # [1] 1 3 2 # # [[3]] # [1] 3 1 2 # # [[4]] # [1] 3 2 1 # # [[5]] # [1] 2 3 1 # # [[6]] # [1] 2 1 3 |

permut <- permn(3) # Create list of permutations permut # Print list of permutations # [[1]] # [1] 1 2 3 # # [[2]] # [1] 1 3 2 # # [[3]] # [1] 3 1 2 # # [[4]] # [1] 3 2 1 # # [[5]] # [1] 2 3 1 # # [[6]] # [1] 2 1 3

The previous output of the RStudio console shows a list that contains all possible permutations of the values 1, 2, and 3.

## Example 2: Count Number of Possible Permutations

Example 2 illustrates how to get the number of possible permutations of a vector object.

For this, we can use the length function in combination with the permn function:

permut_count <- length(permn(3)) # Count permutations permut_count # Print count of permutations # [1] 6 |

permut_count <- length(permn(3)) # Count permutations permut_count # Print count of permutations # [1] 6

There exist six possible permutations of the values 1, 2, and 3.

## Example 3: Create Matrix Containing All Possible Combinations

In Example 3, I’ll illustrate how to create a matrix of all possible combinations with a particular length of a vector of values.

For this, we can use the combn function of the combinat package.

Note that the combn function is also provided by the utils package, which is part of Base R. However, in this example we are using the combn function of combinat:

combi <- combinat::combn(3, 2) # Create matrix of combinations combi # Print matrix of combinations |

combi <- combinat::combn(3, 2) # Create matrix of combinations combi # Print matrix of combinations

The previous matrix illustrates all possible combinations with a length of two of the values 1, 2, and 3.

## Example 4: Count Number of Possible Combinations

Example 4 illustrates how to count the number of possible combinations.

For this, we can apply the ncol function together with the combn function:

combi_count <- ncol(combinat::combn(3, 2)) # Count combinations combi_count # Print count of combinations # [1] 3 |

combi_count <- ncol(combinat::combn(3, 2)) # Count combinations combi_count # Print count of combinations # [1] 3

As you can see, there exist three possible combinations.

## Video & Further Resources

Would you like to know more about combinations and permutations? Then I can recommend having a look at the following video of my YouTube channel. In the video, I show the R codes of this tutorial.

*The YouTube video will be added soon.*

In addition, you could read the related tutorials on statisticsglobe.com.

This article showed how to **calculate permutations and combinations** in the R programming language. In case you have any further questions, please let me know in the comments.

**5**/

**5**(

**1**vote )

### Subscribe to my free statistics newsletter: