lookup: Lookup in a dictionary

Description Usage Arguments Value Examples

View source: R/purrr-like.R

Description

Looks up all values as keys of the dictionary and returns the values.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
lookup(dict, ..., default = NA, dict_key_is_regex = F, key_is_regex = F)

lookup_int(dict, ..., default = NA, dict_key_is_regex = F, key_is_regex = F)

lookup_chr(dict, ..., default = NA, dict_key_is_regex = F, key_is_regex = F)

lookup_lgl(dict, ..., default = NA, dict_key_is_regex = F, key_is_regex = F)

lookup_dbl(dict, ..., default = NA, dict_key_is_regex = F, key_is_regex = F)

lookup_num(dict, ..., default = NA, dict_key_is_regex = F, key_is_regex = F)

Arguments

dict

A dictionaryish vector (named: key -> value)

...

Keys to lookup in the dictionary

default

Default value to return if key is not found. Can be a value or function (called with the key). Note: default is to return NA; another very intuitive case is to return the key itself. To achieve this, pass default = identity.

dict_key_is_regex

Should the dictionary keys, the names of dict, be regarded as regular expressions? (excludes key_is_regex)

key_is_regex

Should the keys to lookup be regarded as regular expressions? (excludes dict_key_is_regex)

Value

A list of the same size as ..., containing the lookup results. For the type-specific functions, returns a vector typed as requested, requiring all lookup results to have matching type.

Examples

1
2
3
4
5
6
a <- list("x", "y", "z")
dict <- c(x="xc", y="yv")
# returns c("xc", "yv", na_chr)
lookup_chr(dict, a)#'
# returns c("xc", "yv", "z")
lookup_chr(dict, "x", "y", "z", default=identity)

Example output

               
"xc" "yv"   NA 
               
"xc" "yv"  "z" 

tidytidbits documentation built on March 8, 2021, 5:07 p.m.