Search code examples
rdplyrstrsplit

filtering in dplyr between two columns where one is a list or vector


trying to filter this df down by comparing these two columns where prod exists in the lob column:

reproducible code:

df <- data.frame(prod = c("CES", "Access", "Access", "CES"), lob = c("Access;Entertainment", "CES", "Access", "Access;Entertainment;CES"))

    prod                      lob
1    CES     Access;Entertainment
2 Access                      CES
3 Access                   Access
4    CES Access;Entertainment;CES

Expected Result:

    prod                      lob
1 Access                   Access
2    CES Access;Entertainment;CES

I've tried splitting the lob column into a vector, or a list containing elements, and then used dplyr filter with grepl(prod, lob), or prod %in% lob, but neither seem to work

df %>%
filter(prod %in% lob)

df %>%
mutate(lob = strsplit(lob, ";")) %>%
filter(prod %in% lob)

df %>%
mutate(lob = strsplit(lob, ";")) %>%
filter(grepl(prod), lob)

Solution

  • It would probably be easiest just to add a rowwise() in there

    df %>%
      mutate(lob = strsplit(lob, ";")) %>% 
      rowwise() %>% 
      filter(prod %in% lob) %>% 
      as.data.frame() # rowwise makes it a tibble, this changes it back if needed
    

    If you don't really want to do the mutate(), you can do

    df %>%
      rowwise() %>% 
      filter(prod %in% strsplit(lob, ";")[[1]])