Looking to combine grepl and !grepl in a nested ifelse statement

Hi,

Ive only been using R for a short period of time and this is my first post so apologies if i have tagged/catagorised wrong.

My problem is that i have to create a new variable depending on what words are within other variables. Some of these are very specific e.g. if UTM.source = "PR" then Channel = "PR". However some are more complex e.g. If UTM. source or UTM. medium contain "print" but don't contain "local" then Channel = "DM". I have been trying to use a nested ifelse statement with grepl && !grepl but no variations are working correctly. I should give my whole code, but here are a few lines to show roughly what i have been trying. I am getting no error messages but the output isnt giving me what i need.

mutate(Channel = ifelse(grepl("Email", UTM.source, ignore.case = TRUE) && !grepl("wholesale" , UTM.source, ignore.case = TRUE)
| grepl("Email", UTM.medium, ignore.case = TRUE) && !grepl("wholesale", UTM.medium, ignore.case = TRUE), "Email",
ifelse(grepl("Print", UTM.source, ignore.case = TRUE) | grepl ("print", UTM.medium, ignore.case = TRUE)
&& !grepl("local" , UTM.source, ignore.case = TRUE) | !grepl("local", UTM.medium, ignore.case = TRUE), "DM",

im essentially hoping there with be a way to say "if var1 or var2 contain string1, but don't contain string2 then var3 = string3

I suggest that you use the case_when() function from dplyr instead of nested ifelse() functions and that you use parentheses to designate the desired logic. I also used str_detect() in place of grepl but that isn't necessary. I am not sure I implemented the logic you intend.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
DF <- tibble(UTM.source = c("sdf Email dsdf", "teww wholesale Email", "lkj print ljk", "print jljlj", "lsdj"),
             UTM.medium = c(";sldkf", "sldfskd", "iern", "local", "tnk Email"))

DF
#> # A tibble: 5 x 2
#>   UTM.source           UTM.medium
#>   <chr>                <chr>     
#> 1 sdf Email dsdf       ;sldkf    
#> 2 teww wholesale Email sldfskd   
#> 3 lkj print ljk        iern      
#> 4 print jljlj          local     
#> 5 lsdj                 tnk Email
DF |> mutate(Channel = case_when(
  (str_detect(UTM.source, "Email") & !str_detect(UTM.source, "wholesale")) |
    (str_detect(UTM.medium, "Email") & !str_detect(UTM.medium, "wholesale")) ~ "Email",
  (str_detect(UTM.source, "print") | str_detect(UTM.medium, "print")) &
    (!str_detect(UTM.source, "local") & !str_detect(UTM.medium, "local")) ~ "DM",
  TRUE ~ "No Cases"
))
#> # A tibble: 5 x 3
#>   UTM.source           UTM.medium Channel 
#>   <chr>                <chr>      <chr>   
#> 1 sdf Email dsdf       ;sldkf     Email   
#> 2 teww wholesale Email sldfskd    No Cases
#> 3 lkj print ljk        iern       DM      
#> 4 print jljlj          local      No Cases
#> 5 lsdj                 tnk Email  Email

Created on 2022-05-09 by the reprex package (v2.0.1)

1 Like

Thank you, that has worked perfectly. I was needing it to ignore cases but have solved that with regex (ignore_case = TRUE)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.