View source: R/multiColumnLookup.R
multiColumnLookup | R Documentation |
Lookup by Matching Values in Multiple Columns
multiColumnLookup(data, lookup, value = NULL, drop = TRUE, includeKeys = FALSE)
data |
data frame for which to lookup values in the |
lookup |
lookup table defining the key values to be matched against
the values in the corresponding columns in |
value |
name of column(s) in |
drop |
logical indicating whether or not to return a vector instead of
a one-column data frame in case that there is only one value column. The
default is |
includeKeys |
logical indicating whether or not to include the key
column(s) in the returned data frame (for more than one |
If value
is of length one and drop = TRUE
) a vector
with as many elements as there are rows in data
is returned.
Otherwise a data frame with as many rows as there are rows in data
and with the columns named in value
is returned.
(persons <- rbind(
noFactorDataFrame(name = "Peter", city = "Berlin"),
noFactorDataFrame(name = "Paul", city = "Paris"),
noFactorDataFrame(name = "Mary", city = "Berlin"),
noFactorDataFrame(name = "Paul", city = "Berlin"),
noFactorDataFrame(name = "Peter", city = "Paris")
))
# Who is cool, which city is cool and which combination is coolest?
(is_cool <- kwb.utils::safeRowBindAll(list(
noFactorDataFrame(name = "Paul", city = "Berlin", value = "astro-cool"),
noFactorDataFrame(city = "Berlin", value = "cool"),
noFactorDataFrame(name = "Paul", value = "mega-cool"),
noFactorDataFrame(city = "Paris", value = "ca va")
)))
# Lookup the coolness based on name and city
coolness <- multiColumnLookup(persons, is_cool, value = "value")
# Add the coolness column
cbind(persons, coolness)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.