# Calculate Combinations and Permutations in R

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

 `# using required libraries ` `library``(combinat) ` ` `  `# generating combinations of the  ` `# alphabets taking 2 at a time ` `print ``(``"Combination of letters two at a time"``) ` `combn``(``letters``[1:4], 2) `

Output

``` "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" ```

Example 2:

## R

 `# using required libraries ` `library``(combinat) ` ` `  `# generating combinations of the ` `# alphabets taking 2 at a time ` `print ``(``"Combination where x=m"``) ` ` `  `# selecting 8 items out of 8  ` `combn``(8,8) `

Output

``` "Combination where x=m"
> #selecting 8 items out of 8
> combn(8,8)
 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 where m=1"``) ` ` `  `# selecting 1 items out of 10  ` `combn``(10,1) `

Output

 “Combination where m=1”

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

[1,]    1    2    3    4    5    6    7    8    9    10

The combn() method also provides a count of the number of combinations that will be generated as the output. Since the output is obtained in the form of a matrix, the final result is the number of columns in the matrix, given by ncol(res), where res is the result of combn() method application.

Example 4:

## 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

``` "Number of combinations : "
> print (ncol(res))
 6```

permn() method in R generates all permutations of the elements of x. If x is a positive integer, returns all permutations of the elements of seq(x). The permutation of the number 0 is 1.

Syntax:

permn(x, fun=NULL, …)

Parameters :

• x – vector source for combinations
• fun – function to be applied to each permutation (may be null)

Example 1:

## R

 `# using required libraries ` `library``(combinat) ` ` `  `# generating permutations  ` `# of the numbers 3 ` `print ``(``"Permutations of 3"``) ` `permn``(3) `

Output

``` "Permutations of 3"
[]  1 2 3
[]  1 3 2
[]  3 1 2
[]  3 2 1
[]  2 3 1
[]  2 1 3```

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

Example 2:

## R

 `# using required libraries ` `library``(combinat) ` ` `  `# declaring a list ` `x <- ``c``(``'red'``, ``'blue'``, ``'green'``, ``'violet'``) ` `print ``(``"Permutations of vector x"``) ` `permn``(x) `

Output:

``` "Permutations of vector x"
> permn(x)
[]  "red"    "blue"   "green"  "violet"
[]  "red"    "blue"   "violet" "green"
[]  "red"    "violet" "blue"   "green"
[]  "violet" "red"    "blue"   "green"
[]  "violet" "red"    "green"  "blue"
[]  "red"    "violet" "green"  "blue"
[]  "red"    "green"  "violet" "blue"
[]  "red"    "green"  "blue"   "violet"
[]  "green"  "red"    "blue"   "violet"
[]  "green"  "red"    "violet" "blue"
[]  "green"  "violet" "red"    "blue"
[]  "violet" "green"  "red"    "blue"
[]  "violet" "green"  "blue"   "red"
[]  "green"  "violet" "blue"   "red"
[]  "green"  "blue"   "violet" "red"
[]  "green"  "blue"   "red"    "violet"
[]  "blue"   "green"  "red"    "violet"
[]  "blue"   "green"  "violet" "red"
[]  "blue"   "violet" "green"  "red"
[]  "violet" "blue"   "green"  "red"
[]  "violet" "blue"   "red"    "green"
[]  "blue"   "violet" "red"    "green"
[]  "blue"   "red"    "violet" "green"
[]  "blue"   "red"    "green"  "violet" ```

Similar to combn() method, the number of permutations can also be determined by using the length() method over the output vector generated by permn() method.

Example 3:

## R

 `# using required libraries ` `library``(combinat) ` ` `  `# declaring a list ` `x <- ``c``(``'red'``, ``'blue'``, ``'green'``, ``'violet'``) ` `print ``(``"Permutations of vector x"``) ` ` `  `res <- ``permn``(x) ` `print ``(``"Number of possible permutations : "``) ` `print ``(``length``(res)) `

Output

 “Number of possible permutations : ”

> print (length(res))

 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

 `library``(gtools) ` `# describing the input vector ` ` `  `vec<- ``LETTERS``[4:7] ` ` `  `# getting permutations on choose 2  ` `# letters out of 4 letters without  ` `# replacement ` `res <- ``permutations``(n=4,r=2,v=vec) ` `print ``(``"Permutations without replacement"``) ` `print ``(res) ` ` `  `print ``(``"Number of permutations without replacement"``) ` `print ``(``nrow``(res)) ` ` `  `# calculating permutations with replacement ` `res1 <- ``permutations``(n=4,r=2,v=vec,repeats.allowed=T) ` `print ``(``"Permutations with replacement"``) ` `print ``(res1) ` ` `  `print ``(``"Number of permutations with replacement"``) ` `print ``(``nrow``(res1)) `

Output

``` "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"
 "Number of permutations without replacement"
 12
 "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"
 "Number of permutations with replacement"
 16```

Similarly, the combinations method can be used to generate possible combinations from the specified source vector. All the arguments are similar to the permutations() method.

Syntax:

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

It is not mandatory to specify the input vector.

Example 2:

## R

 `library``(gtools) ` ` `  `# generating combinations of the  ` `# alphabets taking 2 at a time ` `print ``(``"Combination of five objects taken two at a time"``) ` `combinations``(5, 2) `

Output

``` "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

``` "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
 "Number of combinations without repetition"
 6
 "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
 "Number of combinations with repetition"
 10```

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!