The lookup()
function implements lookup of certain strings (such as
variable names) from a lookup table which maps keys onto values (such as
variable labels or descriptions).
The lookup table can be in the form of a two-column data.frame
, in the form
of a named vector
, or in the form of a list
. If the table is in the form
of a data.frame
, the lookup columns should be named name
(for the key)
and value
(for the value). If the lookup table is in the form of a named
vector
or list
, the name is used for the key, and the returned value is
taken from the values in the vector or list.
Original values are returned if they are not found in the lookup table.
Alternatively, a default
can be specified for values that are not found.
Note that an NA
in x will never be found and will be replaced with the
default value. To specify different defaults for values that are not found
and for NA
values in x
, the default
must be crafted manually to achieve
this.
Any names in x are not included in the result.
The lookuper()
function returns a function equivalent to the lookup()
function, except that instead of taking a lookup table as an argument, the
lookup table is embedded in the function itself.
This can be very useful, in particular when using the lookup function as an
argument to other functions that expect a function which maps
character
->character
but do not offer a good way to pass additional
arguments to that function.