Learn R Programming

qdap (version 0.2.5)

hash: Hash/Dictionary Lookup

Description

Creates a new environment for quick hash style dictionary lookup.

Usage

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

Arguments

x
A two column dataframe.
mode.out
The type of output (column 2) expected (e.g., "character", "numeric", etc.)

Value

  • Creates a "hash table" or a two column data frame in its own environment.

References

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

See Also

lookup, environment

Examples

Run this code
(DF <- aggregate(mpg~as.character(carb), mtcars, mean))
new.hash <- hash(DF)  #numeric outcome
sapply(as.character(mtcars$carb), function(x) {
    if(exists(x, envir = new.hash)) {
        get(x, envir = new.hash)
    } else {
        NA
    }
})

new.hash <- hash(DF, "character") #character outcome
sapply(as.character(mtcars$carb), function(x) {
    if(exists(x, envir = new.hash)) {
        get(x, envir = new.hash)
    } else {
        NA
    }
})

Run the code above in your browser using DataLab