Here, we discuss factorials, permutation and combination in R using
the factorial()
, choose()
,
combn()
and permn()
functions.
Function | Usage | Package |
factorial() |
Calculate factorial value (\(n!\)) | base |
choose() |
Calculate number of ways to choose \(k\) items from \(n\) items | base |
combn() |
List all the ways to choose \(k\) items from \(n\) items | utils |
permn() |
Lists all the possible arrangement of \(n\) items | combinat |
All functions come with the "base" or "utils" package in the base
version of R, except permn()
which requires installation of
the "combinat" package.
\(n! = n \times (n-1) \times ... \times 2
\times 1\) is represented by factorial(n)
.
Examples:
[1] 6
[1] 720
Calculate the number of ways to choose \(k\) items from \(n\) items, \(^nC_k = \frac{n!}{k!(n-k)!}\) is
represented by choose(n, k)
.
Examples:
[1] 10
[1] 252
To list all the ways to choose \(k\) items from \(n\) items, use the
combn()
function.
Examples:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 1 2 2 2 3 3 4
[2,] 2 3 4 5 3 4 5 4 5 5
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "A" "A" "A" "B" "B" "C"
[2,] "B" "C" "D" "C" "D" "D"
[,1] [,2] [,3] [,4]
[1,] "A" "A" "A" "B"
[2,] "B" "B" "C" "C"
[3,] "C" "D" "D" "D"
Calculate the number of ways to arrange \(n\) different items, which is \(n!\).
Examples:
[1] 24
[1] 3628800
To list all the ways to arrange \(n\) different items, use the
permn()
function after installing the "combinat"
package.
Examples:
[[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
[[1]]
[1] "A" "B" "C"
[[2]]
[1] "A" "C" "B"
[[3]]
[1] "C" "A" "B"
[[4]]
[1] "C" "B" "A"
[[5]]
[1] "B" "C" "A"
[[6]]
[1] "B" "A" "C"
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes