Hash/Dictionary Lookup

Usage

hash(x)
hash_look(terms, key, missing = NA)
terms %hl% key
terms %hl+% key
hash_e(x, mode.out = "numeric")

Arguments

x
A two column dataframe.
terms
A vector of terms to undergo a lookup.
key
The hash key to use.
missing
Value to assign to terms not found in the hash table.
mode.out
The type of output (column 2) expected (e.g., "character", "numeric", etc.)

Value

hash - Creates a "hash table", a two column data.table.

hash_e - Creates a "hash table", a two column data.frame in its own environment.

Description

hash - Creates a data.table based hash table for quick hash style dictionary lookup.

hash_look - Works with a hash table such as is returned from hash, to lookup values.

%hl% - A binary operator version of hash_look.

%hl+% - A binary operator version of hash_look for when missing is assumed to be NULL.

hash_e - Creates a new environment for quick hash style dictionary lookup.

Examples

##===================## ## data.table Hashes ## ##===================## (DF <- aggregate(mpg~as.character(carb), mtcars, mean))
as.character(carb) mpg 1 1 25.34286 2 2 22.40000 3 3 16.30000 4 4 15.79000 5 6 19.70000 6 8 15.00000
x <- sample(DF[, 1], 20, TRUE) new.hash <- hash(DF) x2 <- c(9, 12, x) hash_look(x, new.hash)
[1] 15.00000 15.79000 15.79000 16.30000 16.30000 19.70000 19.70000 19.70000 19.70000 15.79000 22.40000 19.70000 22.40000 [14] 25.34286 15.79000 16.30000 16.30000 19.70000 15.00000 15.00000
x %hl% new.hash
[1] 15.00000 15.79000 15.79000 16.30000 16.30000 19.70000 19.70000 19.70000 19.70000 15.79000 22.40000 19.70000 22.40000 [14] 25.34286 15.79000 16.30000 16.30000 19.70000 15.00000 15.00000
x2 %hl% new.hash
[1] NA NA 15.00000 15.79000 15.79000 16.30000 16.30000 19.70000 19.70000 19.70000 19.70000 15.79000 22.40000 [14] 19.70000 22.40000 25.34286 15.79000 16.30000 16.30000 19.70000 15.00000 15.00000
x2 %hl+% new.hash
[1] 9.00000 12.00000 15.00000 15.79000 15.79000 16.30000 16.30000 19.70000 19.70000 19.70000 19.70000 15.79000 22.40000 [14] 19.70000 22.40000 25.34286 15.79000 16.30000 16.30000 19.70000 15.00000 15.00000
## Create generic functions hfun <- function(x, ...) { hsh <- hash(x, ...) function(x, ...) hash_look(x, hsh, ...) } m <- hfun(DF) m(x)
[1] 15.00000 15.79000 15.79000 16.30000 16.30000 19.70000 19.70000 19.70000 19.70000 15.79000 22.40000 19.70000 22.40000 [14] 25.34286 15.79000 16.30000 16.30000 19.70000 15.00000 15.00000
##====================## ## Environment Hashes ## ##====================## new.hash2 <- hash_e(DF) x %hl% new.hash2
[1] 15.00000 15.79000 15.79000 16.30000 16.30000 19.70000 19.70000 19.70000 19.70000 15.79000 22.40000 19.70000 22.40000 [14] 25.34286 15.79000 16.30000 16.30000 19.70000 15.00000 15.00000
x2 %hl% new.hash2
[1] NA NA 15.00000 15.79000 15.79000 16.30000 16.30000 19.70000 19.70000 19.70000 19.70000 15.79000 22.40000 [14] 19.70000 22.40000 25.34286 15.79000 16.30000 16.30000 19.70000 15.00000 15.00000
x2 %hl+% new.hash2
[1] 9.00000 12.00000 15.00000 15.79000 15.79000 16.30000 16.30000 19.70000 19.70000 19.70000 19.70000 15.79000 22.40000 [14] 19.70000 22.40000 25.34286 15.79000 16.30000 16.30000 19.70000 15.00000 15.00000

See also

setDT, hash environment

Author

hash_e - Bryan Goodrich and Tyler Rinker .