The lookup() function implements lookup of values (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 key column should be named either key or name, and
the value column should be named value (for the value). If the lookup table
is in the form of a named vector or list, the names are used as the key,
and the returned value is taken from the values in the vector or list.
The underlying lookup is done using base::match(), and all atomic data
types except factor are supported. Factors are omitted due to the ambiguity
in what should be looked up (the values or the levels). It is important that
x, .default and the columns of lookup_table are all of the same type
(specifically of the same base::mode()). If the lookup table is specified
as a vector or list, only the character variables are supported,
because name(lookup_table) is always of mode character.
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 it is possible to specify NA as one of the keys to look up
NA values (only when using a data.frame as lookup table).
Any names or attributes of x are preserved.
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 (or other data types), but do not offer a good way
to pass additional arguments to that function.