Calculate Combinations and Permutations in R
Last Updated :
30 Jul, 2024
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
Similar Reads
Permutations and Combinations
Permutation and Combination are the most fundamental concepts in mathematics related to picking items from a group or set. Permutation is arranging items considering the order of selection from a certain group.Combination is selecting items without considering order.For example, in the below diagram
14 min read
Difference between Permutation and Combination
Permutations and combinations are two important concepts in mathematics used for counting and solving problems involving arrangements or selections. The key difference between them is whether the order of items matters. In permutations, the order is important, while in combinations, it is not.For ex
6 min read
Find the number of permutations and combinations if n = 12 and r = 2
Permutation is known as the process of organizing the group, body, or numbers in order, selecting the body or numbers from the set, is known as combinations in such a way that the order of the number does not matter.In mathematics, permutation is also known as the process of organizing a group in wh
3 min read
Class 11 NCERT Solutions - Chapter 7 Permutations And Combinations - Exercise 7.2
Chapter 7 of the Class 11 NCERT Mathematics textbook, titled "Permutations and Combinations," delves into the principles of counting and arranging objects. This chapter covers the concepts of permutations and combinations, which are crucial for solving problems involving the arrangement and selectio
3 min read
Class 11 NCERT Solutions - Chapter 7 Permutations And Combinations - Exercise 7.1
Chapter 7 of the Class 11 NCERT Mathematics textbook, titled "Permutations and Combinations," introduces students to fundamental concepts in counting and arranging objects. This chapter focuses on the principles of permutations and combinations, which are essential for solving various problems in pr
5 min read
Tricks to Solve Permutation and Combination Questions
Permutations and combinations are essential topics in probability and quantitative aptitude, widely applicable in both competitive exams and everyday problem-solving. Mastering these concepts will help you solve problems related to arrangements and selections, which are common in various competitive
4 min read
Calculate Correlation Matrix Only for Numeric Columns in R
A correlation matrix is a tabular representation of the relation between numeric attributes of a dataframe. The values present in the table are correlation coefficients between the attributes. Dataset used: bestsellers To create a correlation matrix cor() function is called with the dataframe as an
2 min read
Class 11 NCERT Mathematics Solutions- Chapter 7 Permutations And Combinations - Exercise 7.4
Chapter 7 of the Class 11 NCERT Mathematics textbook, "Permutations and Combinations," explores fundamental concepts of counting and arrangement. Exercise 7.4 focuses on applying these concepts to solve problems related to permutations and combinations, enhancing students' ability to handle various
6 min read
Operations on Matrices in R
Matrices in R are a bunch of values, either real or complex numbers, arranged in a group of fixed number of rows and columns. Matrices are used to depict the data in a structured and well-organized format. It is necessary to enclose the elements of a matrix in parentheses or brackets. A matrix with
8 min read
How to Calculate Correlation Between Multiple Variables in R?
In this article, we will discuss how to calculate Correlation between Multiple variables in R Programming Language. Correlation is used to get the relation between two or more variables: The result is 0 if there is no correlation between two variablesThe result is 1 if there is a positive correlatio
4 min read