How to Create Combinations and Permutations of Vectors in R

Jesse John Feb 02, 2024
  1. the Binomial Coefficient and Combinations
  2. the Factorial and Permutations
  3. Combinations of Elements of One Vector
  4. Combinations of Elements of Many Vectors
  5. Permutations of Elements of a Vector
  6. Dealing With Non-Unique Elements
  7. Conclusion
How to Create Combinations and Permutations of Vectors in R

Base R provides functions to help us compute binomial coefficients and find the numbers of permutations. However, sometimes we want to find all the combinations and permutations, not just get their number.

This article will discuss getting the number of combinations and permutations and getting all the combinations and permutations. We’ll also discuss what happens when the vectors have duplicate elements.

Note that these functions work only for a limited range of values.

the Binomial Coefficient and Combinations

The choose(n,k) function computes the binomial coefficient. We get the number of combinations of n unique items taken k at a time without repetition.

Example Code:

# Combinations
# Count Combinations of 2 Objects From 20 Objects
choose(20,2)

# It Does Not Work for Very Large Values
choose(1030,500)

Output:

> # Count combinations of 2 objects from 20 objects.
> choose(20,2)
[1] 190

> # It does not work for very large values.
> choose(1030,500)
[1] Inf

the Factorial and Permutations

The factorial(x) function gives us the number of permutations of x unique objects without repetition. There is no specific function in base R for counting the permutations of k objects from a set of n objects, but it can be computed as the quotient of factorial(n) and factorial(k).

Example Code:

# Permutations
# Permutations of 17 Distinct Objects
factorial(17)

# It Does Not Work for Large Values
factorial(171)

# Permutations of 5 Objects From 19 Distinct Objects
factorial(19)/factorial(5)

Output:

> # Permutations of 17 distinct objects.
> factorial(17)
[1] 3.556874e+14

> # It does not work for large values.
> factorial(171)
[1] Inf

> # Permutations of 5 objects from 19 distinct objects.
> factorial(19)/factorial(5)
[1] 1.013709e+15

Combinations of Elements of One Vector

The function combn(x,m) lists all the combinations of m objects drawn from x distinct objects without repetition. The columns are different combinations.

Example Code:

# the Objects Vector
mv1 = c("Tennis", "Badminton", "Football", "Athletics", "Chess")
mv1

# All Combinations of Two Elements
combn(mv1,2)

# All Combinations of Three Elements
combn(mv1,3)

# Check Its Properties
c3 = combn(mv1,3)
class(c3)
names(c3)

Output:

> mv1
[1] "Tennis"    "Badminton" "Football"  "Athletics" "Chess"

> # All combinations of two elements.
> combn(mv1,2)
     [,1]        [,2]       [,3]        [,4]     [,5]        [,6]        [,7]        [,8]        [,9]
[1,] "Tennis"    "Tennis"   "Tennis"    "Tennis" "Badminton" "Badminton" "Badminton" "Football"  "Football"
[2,] "Badminton" "Football" "Athletics" "Chess"  "Football"  "Athletics" "Chess"     "Athletics" "Chess"
     [,10]
[1,] "Athletics"
[2,] "Chess"

> # Check its properties.
> c3 = combn(mv1,3)

> class(c3)
[1] "matrix" "array"

> names(c3)
NULL

The output is an array. There are no column or row names.

Combinations of Elements of Many Vectors

The expand.grid() function enables us to create a data frame with all combinations of elements of the given vectors. The function can be given several vectors as input.

Again, the input vectors must have distinct elements for the function to give the desired output.

Example Code:

# Create a New Vector
mv2 = c("Apple", "Mango", "Carrot")
mv2

# the expand.grid() Function Lists All Combinations of the Elements of the Given Vectors
# Use Existing Vectors, or Create a Vector Using C()
ex_df = expand.grid(Sport = mv1, Fruit = mv2, Color = c("Blue", "Red"))
ex_df

Output:

> mv2
[1] "Apple"  "Mango"  "Carrot"

> # The expand.grid() function lists all combinations of the elements of the given vectors.
> # Use existing vectors or create a vector using c().
> ex_df = expand.grid(Sport = mv1, Fruit = mv2, Color = c("Blue", "Red"))
> ex_df
       Sport  Fruit Color
1     Tennis  Apple  Blue
2  Badminton  Apple  Blue
3   Football  Apple  Blue
4  Athletics  Apple  Blue
5      Chess  Apple  Blue
6     Tennis  Mango  Blue
7  Badminton  Mango  Blue
8   Football  Mango  Blue
9  Athletics  Mango  Blue
10     Chess  Mango  Blue
11    Tennis Carrot  Blue
12 Badminton Carrot  Blue
13  Football Carrot  Blue
14 Athletics Carrot  Blue
15     Chess Carrot  Blue
16    Tennis  Apple   Red
17 Badminton  Apple   Red
18  Football  Apple   Red
19 Athletics  Apple   Red
20     Chess  Apple   Red
21    Tennis  Mango   Red
22 Badminton  Mango   Red
23  Football  Mango   Red
24 Athletics  Mango   Red
25     Chess  Mango   Red
26    Tennis Carrot   Red
27 Badminton Carrot   Red
28  Football Carrot   Red
29 Athletics Carrot   Red
30     Chess Carrot   Red

We get a data frame in which the columns correspond to the vectors, and the rows are combinations of the elements. The number of rows is the product of the number of elements in each vector.

Permutations of Elements of a Vector

The permn() function from the combinat package creates permutations of all the elements of a vector. This package may need to be installed.

Example Code:

# Install the Combinat Package, if It Is Not Available
# Uncomment and Run the Following Line to Install
# install.packages("combinat")

# Load the Combinat Package
library(combinat)

# Create All Permutations of a Vector
permn(mv2)

Output:

> # Create all permutations of a vector.
> permn(mv2)
[[1]]
[1] "Apple"  "Mango"  "Carrot"

[[2]]
[1] "Apple"  "Carrot" "Mango"

[[3]]
[1] "Carrot" "Apple"  "Mango"

[[4]]
[1] "Carrot" "Mango"  "Apple"

[[5]]
[1] "Mango"  "Carrot" "Apple"

[[6]]
[1] "Mango"  "Apple"  "Carrot"

Dealing With Non-Unique Elements

Let’s see what happens if some elements of a vector are identical. We will look at the output of all three functions.

Example Code:

# Example for Combn()
# Vector With Some Identical Elements
mv3 = c("Tennis", "Tennis", "Football", "Athletics")
mv3
# Resulting Combinations
c2 = combn(mv3,2)
c2

# Example for expand.grid()
mv4 = c("Red", "Blue", "Red")
eg = expand.grid(Sport = mv3, Color = mv4)
eg

# Example for Permn()
pn = permn(mv4)
pn

Output:

# Example for combn().
> mv3
[1] "Tennis"    "Tennis"    "Football"  "Athletics"
> # Resulting combinations.
> c2 = combn(mv3,2)
> c2
     [,1]     [,2]       [,3]        [,4]       [,5]        [,6]
[1,] "Tennis" "Tennis"   "Tennis"    "Tennis"   "Tennis"    "Football"
[2,] "Tennis" "Football" "Athletics" "Football" "Athletics" "Athletics"

> # Example for expand.grid().
> mv4 = c("Red", "Blue", "Red")
> eg = expand.grid(Sport = mv3, Color = mv4)
> eg
       Sport Color
1     Tennis   Red
2     Tennis   Red
3   Football   Red
4  Athletics   Red
5     Tennis  Blue
6     Tennis  Blue
7   Football  Blue
8  Athletics  Blue
9     Tennis   Red
10    Tennis   Red
11  Football   Red
12 Athletics   Red

> # Example for permn().
> pn = permn(mv4)
> pn
[[1]]
[1] "Red"  "Blue" "Red"

[[2]]
[1] "Red"  "Red"  "Blue"

[[3]]
[1] "Red"  "Red"  "Blue"

[[4]]
[1] "Red"  "Blue" "Red"

[[5]]
[1] "Blue" "Red"  "Red"

[[6]]
[1] "Blue" "Red"  "Red"

Identical elements are combined with other elements in the combn() output. The output of expand.grid() has identical rows, the identical elements appear in each row of the output of permn(), and some rows are also identical.

If this is not the output we want, we need to do further data processing. Generally, the easiest option is to ensure that the vectors we use have unique elements, and we can use the unique() function for this purpose.

Example Code:

# Output After Removing Duplicates

# Combn()
c2_u = combn(unique(mv3),2)
c2_u

# expand.grid()
eg_u = expand.grid(Sport = unique(mv3), Color = unique(mv4))
eg_u

# Permn()
pn_u = permn(unique(mv4))
pn_u

Output:

> # combn()
> c2_u = combn(unique(mv3),2)
> c2_u
     [,1]       [,2]        [,3]
[1,] "Tennis"   "Tennis"    "Football"
[2,] "Football" "Athletics" "Athletics"

> # expand.grid()
> eg_u = expand.grid(Sport = unique(mv3), Color = unique(mv4))
> eg_u
      Sport Color
1    Tennis   Red
2  Football   Red
3 Athletics   Red
4    Tennis  Blue
5  Football  Blue
6 Athletics  Blue

> # permn()
> pn_u = permn(unique(mv4))
> pn_u
[[1]]
[1] "Red"  "Blue"

[[2]]
[1] "Blue" "Red"

Conclusion

When we need to create data for analysis or illustration, the ability to create all conceivable combinations of elements in a vector or several vectors comes in handy.

In this article, we saw how easy it is to generate combinations and permutations of vectors in R and to calculate the numbers of such combinations and permutations.

We also looked at the effect of identical elements in vectors and saw one way to deal with the issue.

Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.

Related Article - R Vector