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:
I think the following does most of what you want. Notice that I changed the values of url. I don't think str_detect will find part of the pattern within the string but it will find the pattern as part of the string.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(stringr)
example_df <- data.frame(
url = c('blog/blah','UK','subscription' ,'blog')#'blog/?utm_medium=foo', 'UK/something'
)
lookup_df <- data.frame(
lookup_string = c('blog/blah', 'subscription', 'UK'),
group = c('blog', 'subs', 'UK')
)
lookup_func <- function(str, lut) {
lut %>% filter(str_detect(lookup_string, str)) %>% head(1) %>% pull(group)
}
example_df %>% rowwise() %>% mutate(blah = lookup_func(url,lookup_df))
#> # A tibble: 4 x 2
#> # Rowwise:
#> url blah
#> <chr> <chr>
#> 1 blog/blah blog
#> 2 UK UK
#> 3 subscription subs
#> 4 blog blog