Description Usage Arguments Details Value Author(s) References See Also Examples
combinations
enumerates the possible combinations of a
specified size from the elements of a vector. permutations
enumerates the possible permutations.
1 2 | combinations(n, r, v=1:n, set=TRUE, repeats.allowed=FALSE)
permutations(n, r, v=1:n, set=TRUE, repeats.allowed=FALSE)
|
n |
Size of the source vector |
r |
Size of the target vectors |
v |
Source vector. Defaults to |
set |
Logical flag indicating whether duplicates should be
removed from the source vector |
repeats.allowed |
Logical flag indicating whether the
constructed vectors may include duplicated values. Defaults to
|
Caution: The number of combinations and permutations increases rapidly
with n
and r
!.
To use values of n
above about 45, you will need to increase
R's recursion limit. See the expression
argument to the
options
command for details on how to do this.
Returns a matrix where each row contains a vector of length r
.
Original versions by Bill Venables
Bill.Venables@cmis.csiro.au. Extended to handle
repeats.allowed
by Gregory R. Warnes
greg@warnes.net.
Venables, Bill. "Programmers Note", R-News, Vol 1/1, Jan. 2001. https://cran.r-project.org/doc/Rnews/
1 2 3 4 5 6 7 8 9 10 | combinations(3,2,letters[1:3])
combinations(3,2,letters[1:3],repeats=TRUE)
permutations(3,2,letters[1:3])
permutations(3,2,letters[1:3],repeats=TRUE)
# To use large 'n', you need to change the default recusion limit
options(expressions=1e5)
cmat <- combinations(300,2)
dim(cmat) # 44850 by 2
|
[,1] [,2]
[1,] "a" "b"
[2,] "a" "c"
[3,] "b" "c"
[,1] [,2]
[1,] "a" "a"
[2,] "a" "b"
[3,] "a" "c"
[4,] "b" "b"
[5,] "b" "c"
[6,] "c" "c"
[,1] [,2]
[1,] "a" "b"
[2,] "a" "c"
[3,] "b" "a"
[4,] "b" "c"
[5,] "c" "a"
[6,] "c" "b"
[,1] [,2]
[1,] "a" "a"
[2,] "a" "b"
[3,] "a" "c"
[4,] "b" "a"
[5,] "b" "b"
[6,] "b" "c"
[7,] "c" "a"
[8,] "c" "b"
[9,] "c" "c"
[1] 44850 2
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.