Description
R-Object to R-Object Hash Maps
Description
Implementation of hash tables (hash sets and hash maps) in R, featuring arbitrary R objects as keys, arbitrary hash and key-comparison functions, and customizable behaviour upon queries of missing keys.
README.md
r2r
r2r
provides a flexible implementation of hash tables in R, allowing for:
- arbitrary R objects as keys and values,
- arbitrary key comparison and hash functions,
- customizable behaviour (throw or return a default value) on missing key exceptions.
Installation
You can install the development version of r2r
from GitHub with:
# install.packages("devtools")
devtools::install_github("vgherard/r2r")
Usage
library(r2r)
m <- hashmap()
# Insert and query a single key-value pair
m[[ "user" ]] <- "vgherard"
m[[ "user" ]]
#> [1] "vgherard"
# Insert and query multiple key-value pairs
m[ c(1, 2, 3) ] <- c("one", "two", "three")
m[ c(1, 3) ]
#> [[1]]
#> [1] "one"
#>
#> [[2]]
#> [1] "three"
# Keys and values can be arbitrary R objects
m[[ lm(mpg ~ wt, mtcars) ]] <- c(TRUE, FALSE, TRUE)
m[[ lm(mpg ~ wt, mtcars) ]]
#> [1] TRUE FALSE TRUE
Getting help
For further details, including an introductory vignette illustrating the features of r2r
hash maps, you can consult the r2r
website. If you encounter a bug, want to suggest a feature or need further help, you can open a GitHub issue.
Comparison with hash
CRAN package {hash}
also offers an implementation of hash tables based on R environments. The two tables below offer a comparison between {r2r}
and {hash}
(for more details, see the benchmarks Vignette)
Feature | r2r | hash |
---|---|---|
Basic data structure | R environment | R environment |
Arbitrary type keys | X | |
Arbitrary type values | X | X |
Arbitrary hash function | X | |
Arbitrary key comparison function | X | |
Throw or return default on missing keys | X | |
Hash table inversion | X |
Features supported by {r2r} and {hash}.
Task | Comparison |
---|---|
Key insertion | {r2r} ~ {hash} |
Key query | {r2r} < {hash} |
Key deletion | {r2r} << {hash} |
Performances of {r2r} and {hash} for basic hash table operations.