Learn R Programming

qdapTools (version 1.3.7)

hash: Hash/Dictionary Lookup

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.

Usage

hash(x)

hash_look(terms, key, missing = NA)

terms %hl% key

terms %hl+% key

hash_e(x, mode.out = "numeric")

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.

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.)

Author

hash_e - Bryan Goodrich and Tyler Rinker <tyler.rinker@gmail.com>.

References

https://www.talkstats.com/showthread.php/22754-Create-a-fast-dictionary

See Also

Examples

Run this code
##===================##
## data.table Hashes ##
##===================##
(DF <- aggregate(mpg~as.character(carb), mtcars, mean))
x <- sample(DF[, 1], 20, TRUE)
new.hash <- hash(DF)
x2 <- c(9, 12, x)
hash_look(x, new.hash)

x %hl% new.hash
x2 %hl% new.hash
x2 %hl+% new.hash

## Create generic functions
hfun <- function(x, ...) {
    hsh <- hash(x, ...)
    function(x, ...) hash_look(x, hsh, ...)
}

m <- hfun(DF)
m(x)

##====================##
## Environment Hashes ##
##====================##
new.hash2 <- hash_e(DF)

x %hl% new.hash2
x2 %hl% new.hash2
x2 %hl+% new.hash2

Run the code above in your browser using DataLab