The hash package provides a fully-functional hash/dictionary for R. It has richer features and finer control than using native R structures like lists or environments and has as a user-friendly interface.

Performance-wise it has similar and sometimes better performance than these structures.

Installation

Latest Release:

install.packages('hash')

Development Version:

install.packages('devtools')
devtools::install_github('decisionpatterns/hash')

Examples

# Create a hash 
h <- hash(a=1, b=2, c=3)
h <- hash( letters[1:3], 1:3 )
h <- hash( list(a=1,b=2,c=3) )

# Keys 
keys(h)

# Values (named list)
values(h)

# Assign to single key hash
h$a <- "foo"
h[['a']] <- "bar"

# Slice
h[ c('a','c') ]

Accessors

## NAMED ACCESS/REPLACEMENT:

 h$x             : returns value of key `x`; 
 h$x <- value    : sets key `x` to value; 
 h$x <- NULL     : deletes key-value pair `x`


## INTERPRETED ACCES/REPLACEMENT:

 h[[x]]          : returns value of key x; `x` is interpreted.
 h[[x]] <- value : sets the values of key `x`; `x` is interpreted.
 h[[x]] <- NULL  : deletes key-value pair `x`; `x` is interpreted.


## HASH SLICING:

 h[]             : returns a copy of h, same as `copy(h)`
 h[x]            : a hash slice of keys

 h[] <- value    : error, undefined key
 h[x] <- value   : set values for keys `x` to `value`(s)
 h[x] <- NULL    : delete keys `x`

Allowable Values

KEYS must be a valid character value and may not be the empty string (""). Keys must be unique.

VALUES can be any R value, vector, object, etc.

Usage Notes

Hashes probably work about how you would expect, but since there are built from R's native environments. There are three things to Remember:

PASS-BY REFERENCE. hashes are environments, special objects in R where only one copy exists globally. When passed as an argument to a function, no local copy is made and any changes to the hash in the functions are reflected globally, i.e. in the caller's namespace.

PERFORMANCE. Hashes are designed to be exceedingly fast using R environment's internal hash table. The hash function is not without its cost. For small data structures, a named lists and vectors will out-perform a hash in nearly every case. After approximately 500+ elements, the performance of the hash becomes faster than native lists and vectors.



decisionpatterns/r-hash documentation built on Feb. 6, 2019, 10:27 p.m.