0

I'm having a lot of trouble trying to get a new column from an existing one that has a long string in it.

Here is what I'm trying using mutate and case_when:

paid_social_fb_insta <- paid_social %>%
  filter(tier_2 == "facebook - instagram") %>%
  mutate(Campaign_Type = case_when(
    tier_3 = "prospecting - prospecting (cbo) - bw ad - 1dc - 4/1" ~ "Conversion",
    tier_3 = "social method - prospecting - prospecting (cbo) - 1dc_12/16" ~ "Conversion",
    tier_3 = "social mountain - retargeting - retargeting (cbo) - 1dc_12/16" ~ "Conversion",
    tier_3 = "social mountain - tof - prospecting | leads (cbo) - 1dc_12/17" ~ "Top of Funnel",
    tier_3 = "social mountain - brand awareness - prospecting (cbo) - n/a_2/15" ~ "Top of Funnel",
    tier_3 = "tof - prospecting | native leads | 7dc 4/19" ~ "Top of Funnel",
    tier_3 = "tof - prospecting | traffic | ooh test | 7dc 5/3" ~ "Top of Funnel",
    tier_3 = "prospecting - prospecting (cbo) - summer promo broad - 7dc - 5/24" ~ "Conversion",
    tier_3 = "tof - prospecting | traffic | summer promo broad women" ~ "Top of Funnel",
    tier_3 = "prospecting - prospecting (cbo) - high volume - 1dc - 3/12" ~ "Conversion",
    tier_3 = "tof - prospecting | traffic lpv | blog content | 7dc 6/4" ~ "Top of Funnel",
    tier_3 =  "tof - prospecting | traffic lpv | link ads | 7dc 6/10" ~ "Top of Funnel",
    tier_3 =  "creative testing - prospecting - 1dc - 6/11/21" ~ "Conversion",
    tier_3 =  "prospecting - prospecting (cbo) - detailed test targeting - 1dc - 4/1" ~ "Conversion"))
    
   

I get this error message:

x LHS of case  ....  must be a logical vector, not a character vector.

So now I'm trying this version instead with a str_replace... but I'm still not getting it to work. String replace isn't working properly - it's just returning the whole string back. Is it because there are hyphens?

paid_social_fb_insta <- paid_social %>%
  filter(tier_2 == "facebook - instagram") %>%
  mutate(new_col = str_replace(tier_3, "prospecting - prospecting (cbo) - broad women - 1dc - 4/1", "Conversion"))

I feel lost here. I feel like I've done the work of categorizing all these long strings into more simple categories but now I can't seem to put that into a new column in R !

hachiko
  • 671
  • 7
  • 20
  • 1
    It's hard to know exactly what the problem is without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your data, but as a general rule you should be using `==` instead of `=` in case_when to check for logical conditions. This might have resulted in your first error. – Rory S Jul 06 '21 at 15:00
  • 1
    well shoot, that fixed it thank you – hachiko Jul 06 '21 at 15:02

0 Answers0