fastcombn.Rd
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.
fastcombn(x, m, FUN = NULL, simplify = TRUE, ...)
combn_prim(x, m, simplify = TRUE)
vector source for combinations, or integer n for x <- seq(n).
number of elements to choose.
function to be applied to each combination; default ‘NULL’ means the identity, i.e., to return the combination (vector of length ‘m’).
logical indicating if the result should be simplified to a matrix; if FALSE, the function returns a list.
Further arguments passed on to FUN
.
A matrix or a list.
Factors x
are accepted.
combn_prim
is a simplified (but faster) version of the combn
function. Does nok take the FUN
argument.
fastcombn
is intended to be a faster version of the combn
function.
x <- letters[1:5]; m <- 3
fastcombn(x, m)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] "a" "a" "a" "a" "a" "a" "b" "b" "b" "c"
#> [2,] "b" "b" "b" "c" "c" "d" "c" "c" "d" "d"
#> [3,] "c" "d" "e" "d" "e" "e" "d" "e" "e" "e"
combn(x, m)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] "a" "a" "a" "a" "a" "a" "b" "b" "b" "c"
#> [2,] "b" "b" "b" "c" "c" "d" "c" "c" "d" "d"
#> [3,] "c" "d" "e" "d" "e" "e" "d" "e" "e" "e"
combn_prim(x, m)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] "a" "a" "a" "a" "a" "a" "b" "b" "b" "c"
#> [2,] "b" "b" "b" "c" "c" "d" "c" "c" "d" "d"
#> [3,] "c" "d" "e" "d" "e" "e" "d" "e" "e" "e"
x <- letters[1:4]; m <- 3
fastcombn(x, m, simplify=FALSE)
#> [[1]]
#> [1] "a" "b" "c"
#>
#> [[2]]
#> [1] "a" "b" "d"
#>
#> [[3]]
#> [1] "a" "c" "d"
#>
#> [[4]]
#> [1] "b" "c" "d"
#>
combn(x, m, simplify=FALSE)
#> [[1]]
#> [1] "a" "b" "c"
#>
#> [[2]]
#> [1] "a" "b" "d"
#>
#> [[3]]
#> [1] "a" "c" "d"
#>
#> [[4]]
#> [1] "b" "c" "d"
#>
combn_prim(x, m, simplify=FALSE)
#> [[1]]
#> [1] "a" "b" "c"
#>
#> [[2]]
#> [1] "a" "b" "d"
#>
#> [[3]]
#> [1] "a" "c" "d"
#>
#> [[4]]
#> [1] "b" "c" "d"
#>
x <- 1:10; m <- 3
fastcombn(x, m, min)
#> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
#> [38] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
#> [75] 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6
#> [112] 6 6 6 6 6 7 7 7 8
combn(x, m, min)
#> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
#> [38] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
#> [75] 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6
#> [112] 6 6 6 6 6 7 7 7 8
x <- factor(letters[1:8]); m <- 5
if (require(microbenchmark)){
microbenchmark(
combn(x, m, simplify=FALSE),
combn_prim(x, m, simplify=FALSE),
fastcombn(x, m, simplify=FALSE),
times=50
)
}
#> Loading required package: microbenchmark
#> Unit: microseconds
#> expr min lq mean median uq
#> combn(x, m, simplify = FALSE) 123.688 134.919 138.7124 137.0185 144.442
#> combn_prim(x, m, simplify = FALSE) 144.962 155.051 176.4381 159.1035 164.040
#> fastcombn(x, m, simplify = FALSE) 145.952 155.780 160.7474 160.5375 163.838
#> max neval cld
#> 159.755 50 a
#> 824.322 50 b
#> 196.749 50 ab