Skip to contents

fastpeRmute: Fast Combinations and Permutations in R

The fastpeRmute package offers efficient functions for computing combinations and permutations with and without repetition in R. It is tailored for high performance and provides functionality that can be faster than both base R and the gtools package.

Installation

To install the latest development version from GitHub:

devtools::install_github("gi0na/fastpeRmute")

Examples

Combinations

Without Repetition

Load the library:

## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
##  dplyr     1.1.4      readr     2.1.5
##  forcats   1.0.0      stringr   1.5.1
##  ggplot2   3.5.1      tibble    3.2.1
##  lubridate 1.9.3      tidyr     1.3.1
##  purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
##  dplyr::filter() masks stats::filter()
##  dplyr::lag()    masks stats::lag()
##  Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Compute combinations without repetition:

combinations(n = 10, r = 4)
## # A tibble: 210 × 4
##       V1    V2    V3    V4
##    <int> <int> <int> <int>
##  1     1     2     3     4
##  2     1     2     3     5
##  3     1     2     3     6
##  4     1     2     3     7
##  5     1     2     3     8
##  6     1     2     3     9
##  7     1     2     3    10
##  8     1     2     4     5
##  9     1     2     4     6
## 10     1     2     4     7
## # ℹ 200 more rows
With Repetition

Compute combinations with repetition:

combinations(n = 10, r = 4, repeats.allowed = T)
## # A tibble: 715 × 4
##       V1    V2    V3    V4
##    <int> <int> <int> <int>
##  1     1     1     1     1
##  2     1     1     1     2
##  3     1     1     1     3
##  4     1     1     1     4
##  5     1     1     1     5
##  6     1     1     1     6
##  7     1     1     1     7
##  8     1     1     1     8
##  9     1     1     1     9
## 10     1     1     1    10
## # ℹ 705 more rows

Permutations

Without Repetition

Compute permutations without repetition:

permutations(n = 4, r = 2)
##       V1 V2
##  [1,]  1  2
##  [2,]  2  1
##  [3,]  1  3
##  [4,]  3  1
##  [5,]  1  4
##  [6,]  4  1
##  [7,]  2  3
##  [8,]  3  2
##  [9,]  2  4
## [10,]  4  2
## [11,]  3  4
## [12,]  4  3
With Repetition

Compute permutations with repetition:

permutations(n = 4, r = 2, repeats.allowed = T)
##       V1 V2
##  [1,]  1  1
##  [2,]  1  2
##  [3,]  1  3
##  [4,]  1  4
##  [5,]  2  1
##  [6,]  2  2
##  [7,]  2  3
##  [8,]  2  4
##  [9,]  3  1
## [10,]  3  2
## [11,]  3  3
## [12,]  3  4
## [13,]  4  1
## [14,]  4  2
## [15,]  4  3
## [16,]  4  4

Using Vectors and Lists

Sometimes, it’s helpful to generate combinations or permutations of specific objects.

Combinations from a Vector

Using the built-in letters vector:

v = letters
combinations(v=v, r = 3)
## # A tibble: 2,600 × 3
##    V1    V2    V3   
##    <chr> <chr> <chr>
##  1 a     b     c    
##  2 a     b     d    
##  3 a     b     e    
##  4 a     b     f    
##  5 a     b     g    
##  6 a     b     h    
##  7 a     b     i    
##  8 a     b     j    
##  9 a     b     k    
## 10 a     b     l    
## # ℹ 2,590 more rows
Permutations from a Vector

Generate permutations from the letters vector:

v = letters[1:4]
permutations(v=v, r = 2)
##       V1  V2 
##  [1,] "a" "b"
##  [2,] "b" "a"
##  [3,] "a" "c"
##  [4,] "c" "a"
##  [5,] "a" "d"
##  [6,] "d" "a"
##  [7,] "b" "c"
##  [8,] "c" "b"
##  [9,] "b" "d"
## [10,] "d" "b"
## [11,] "c" "d"
## [12,] "d" "c"
Combinations from a List
v <- mapply(v1 = letters[1:4], v2 = sample(LETTERS)[1:4], FUN = function(v1, v2) c(v1, v2), SIMPLIFY = FALSE)
combinations(v=v, r = 3, out_format = 'tibble')
## # A tibble: 4 × 3
##   V1        V2        V3       
##   <list>    <list>    <list>   
## 1 <chr [2]> <chr [2]> <chr [2]>
## 2 <chr [2]> <chr [2]> <chr [2]>
## 3 <chr [2]> <chr [2]> <chr [2]>
## 4 <chr [2]> <chr [2]> <chr [2]>
Permutations from a List
permutations(v=v, r = 2, out_format = 'tibble')
## # A tibble: 12 × 2
##    V1        V2       
##    <list>    <list>   
##  1 <chr [2]> <chr [2]>
##  2 <chr [2]> <chr [2]>
##  3 <chr [2]> <chr [2]>
##  4 <chr [2]> <chr [2]>
##  5 <chr [2]> <chr [2]>
##  6 <chr [2]> <chr [2]>
##  7 <chr [2]> <chr [2]>
##  8 <chr [2]> <chr [2]>
##  9 <chr [2]> <chr [2]>
## 10 <chr [2]> <chr [2]>
## 11 <chr [2]> <chr [2]>
## 12 <chr [2]> <chr [2]>

Contributions & Feedback

Feel free to raise issues, contribute to the codebase, or provide any feedback about the package on our GitHub repository.