I was looking for a smart, or "tidier" way, to make use of a lookup table in the tidyverse, but could not find a satisfying solution.
I have a dataset and lookup table:
# Sample data
data <- data.frame(patients = 1:5,
treatment = letters[1:5],
hospital = c("yyy", "yyy", "zzz", "www", "uuu"),
response = rnorm(5))
# Lookup table
lookup <- tibble(hospital = c("yyy", "uuu"), patients = c(1,5))
... where each row in the lookup table is the exact pattern for which I want to filter the first tibble (data).
The wanted result would look like this:
# A tibble: 3 x 4
patients treatment hospital response
<dbl> <chr> <chr> <dbl>
1 1.00 a yyy -0.275
2 5.00 e uuu -0.0967
The easiest solution I came up with is something like this:
as.tibble(dat) %>%
filter(paste(hospital, patients) %in% paste(lookup$hospital, lookup$patients))
However, this must be something that a lot of people regularly do - is there a cleaner and more convienent way to do this (i.e. for more than two columns in your lookup table)?