I would like to lookup a value in df$var to see if it corresponds via str_detect() to a value in another data frame.
example_df <- data.frame(
url = c('blog/blah', 'blog/?utm_medium=foo', 'UK/something')
)
lookup_df <- data.frame(
lookup_string = c('blog/blah', 'subscription', 'UK'),
group = c('blog', 'subs', 'UK')
)
Want to check each value of example_df$url against lookup_df$lookup_string. I can do this for a single example for the first value, I want to see if string 'blog/blah' appears in lookup_df$lookup_string and if so, return the corresponding value in lookup_df$group, which in this case would be 'blog'. I can do this manually one at a time:
str = 'blog'
lut = lookup_df
lut %>% filter(str_detect(lookup_string, str)) %>% head(1) %>% pull(group)
[1] "blog"
Good, that is what I want. However, if I try to make this a function to use within a dplyr chain, in this case with mutate:
lookup_func <- function(str, lut) {
lut %>% filter(str_detect(lookup_string, str)) %>% head(1) %>% pull(group)
}
example_df %>% mutate(blah = lookup_func(url, lookup_df))
Error: Problem with `mutate()` input `blah`.
x Input `blah` can't be recycled to size 3.
ℹ Input `blah` is `lookup_func(url, lookup_df)`.
ℹ Input `blah` must be size 3 or 1, not 0.
How can I use lookup_func() within mutate() in this way?