Open In App

Calculate Combinations and Permutations in R

Improve
Improve
Like Article
Like
Save
Share
Report

Combinatorics is an important aspect of data analysis and statistics. It is used to solve many aptitude-based and real-life problems. While permutations do take into account the order, the combinations are independent of it. Therefore, permutation is considered to be an ordered combination. R language allows us the ability to invoke many packages to compute combinations and permutations. 

Method 1: Combinat package

Combinat package in R programming language can be used to calculate permutations and combinations of the numbers. It provides routines and methods to perform combinatorics. 

combn() method in R language belonging to this package is used to generate all combinations of the elements of x taken m at a time.  If x is a positive integer, returns all combinations of the elements of seq(x) taken m at a time. 

Syntax:

combn(x, m, fun=NULL, simplify=TRUE, …)

Parameters : 

  • x – vector source for combinations
  • m – number of elements to be taken
  • fun – function to be applied to each combination (may be null)
  • simplify – logical, if FALSE, returns a list, otherwise returns vector or array

Example 1:

R
[1] "Combination of letters two at a time" 
> combn(letters[1:4], 2)     
       [,1] [,2] [,3] [,4] [,5] [,6] 
[1,]   "a"  "a"  "a"  "b"  "b"  "c" 
[2,]   "b"  "c"  "d"  "c"  "d"  "d" 

Output

[1] "Combination where x=m" 
> #selecting 8 items out of 8
> combn(8,8)
[1] 1 2 3 4 5 6 7 8

The below code illustrates the method where m=1, that is, the number of chosen items is equivalent to 1. The number of ways to perform this operation is equivalent to the total number of items, since each item can be picked once. 

Example 3:

R
# using required libraries
library(combinat)

# generating combinations of the
# alphabets taking 2 at a time
print ("Combination of letters two at a time")
res <- combn(letters[1:4], 2)

# calculate number of columns in 
# the result 
print ("Number of combinations : ")
print (ncol(res))

Output

[1] "Permutations of 3"
[[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

We can compute the permutations of a vector or a list too. 

Example 2:

R
[1] "Permutations of vector x" 
> permn(x)
[[1]] [1] "red"    "blue"   "green"  "violet" 
[[2]] [1] "red"    "blue"   "violet" "green"   
[[3]] [1] "red"    "violet" "blue"   "green"  
[[4]] [1] "violet" "red"    "blue"   "green"   
[[5]] [1] "violet" "red"    "green"  "blue"    
[[6]] [1] "red"    "violet" "green"  "blue"    
[[7]] [1] "red"    "green"  "violet" "blue"    
[[8]] [1] "red"    "green"  "blue"   "violet"  
[[9]] [1] "green"  "red"    "blue"   "violet"  
[[10]] [1] "green"  "red"    "violet" "blue"    
[[11]] [1] "green"  "violet" "red"    "blue"    
[[12]] [1] "violet" "green"  "red"    "blue"    
[[13]] [1] "violet" "green"  "blue"   "red"     
[[14]] [1] "green"  "violet" "blue"   "red"     
[[15]] [1] "green"  "blue"   "violet" "red"     
[[16]] [1] "green"  "blue"   "red"    "violet"  
[[17]] [1] "blue"   "green"  "red"    "violet"  
[[18]] [1] "blue"   "green"  "violet" "red"     
[[19]] [1] "blue"   "violet" "green"  "red"     
[[20]] [1] "violet" "blue"   "green"  "red"     
[[21]] [1] "violet" "blue"   "red"    "green"   
[[22]] [1] "blue"   "violet" "red"    "green"   
[[23]] [1] "blue"   "red"    "violet" "green"   
[[24]] [1] "blue"   "red"    "green"  "violet" 

Output

[1] “Number of possible permutations : ” 

> print (length(res)) 

[1] 24

Method 2: gtools package

Using gtools package in R programming language can also be used to calculate the permutations and combinations both without and with repetition easily. Combinatorics can be carried out easily using the gtools package in R. 

permutations() method in R can be used to easily calculate the permutations with and without replacement. It returns a data frame or a matrix of the possible permutations formed on shuffling the elements of the specified vector subjected to the constraints. The number of such possible permutations can be captured by using nrow() method, which returns the number of rows in the obtained output data frame. 

Syntax:

permutations(n , r, vec, repeats.allowed=F)

Parameters : 

  • n – no. of objects to choose from
  • r – no. of objects to choose
  • vec – the atomic vector or matrix to shuffle
  • repeats.allowed – By default : false. If true, the permutations are generated with repetition allowed

Return : 

A data frame or matrix with plausible permutations. The number of rows in the data frame is equivalent to the value of r.

Example 1:

R
[1] "Permutations without replacement"
     [,1] [,2]
[1,] "D"  "E"
[2,] "D"  "F"
[3,] "D"  "G"
[4,] "E"  "D"
[5,] "E"  "F"
[6,] "E"  "G"
[7,] "F"  "D"
[8,] "F"  "E"
[9,] "F"  "G"
[10,] "G"  "D"
[11,] "G"  "E"
[12,] "G"  "F"
[1] "Number of permutations without replacement"
[1] 12
[1] "Permutations with replacement"
     [,1] [,2]
[1,] "D"  "D"
[2,] "D"  "E"
[3,] "D"  "F"
[4,] "D"  "G"
[5,] "E"  "D"
[6,] "E"  "E"
[7,] "E"  "F"
[8,] "E"  "G"
[9,] "F"  "D"
[10,] "F"  "E"
[11,] "F"  "F"
[12,] "F"  "G"
[13,] "G"  "D"
[14,] "G"  "E"
[15,] "G"  "F"
[16,] "G"  "G"
[1] "Number of permutations with replacement"
[1] 16

Output

[1] "Combination of five objects taken two at a time"
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 1 5
[5,] 2 3
[6,] 2 4
[7,] 2 5
[8,] 3 4
[9,] 3 5
[10,] 4 5

Example 3:

R
library(gtools)
vec <- c(1:4)

# generating combinations of the 
# digits taking 2 at a time
print ("Combination of four objects taken two at\
a time without repetition")
res<- combinations(n= 4, r = 2, v = vec)
print (res)

print ("Number of combinations without repetition")
print (nrow(res))

print ("Combination of four objects taken two at a \
time with repetition")
res1 <- combinations(n= 4, r = 2, v = vec, repeats.allowed=T)
print (res1)

print ("Number of combinations with repetition")
print (nrow(res1))

Output

[1] "Combination of four objects taken two at a time without repetition"
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 2 3
[5,] 2 4
[6,] 3 4
[1] "Number of combinations without repetition"
[1] 6
[1] "Combination of four objects taken two at a time with repetition"
[,1] [,2]
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 1 4
[5,] 2 2
[6,] 2 3
[7,] 2 4
[8,] 3 3
[9,] 3 4
[10,] 4 4
[1] "Number of combinations with repetition"
[1] 10


Last Updated : 13 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads