Issue with case_when use

Hi,

I am trying to replace a bunch of words in a variable and thought of using case_when. But it doesn't seem to work right for me. Can someone help me out here, please? Example with just two words replacement,

First attempt -

df <- df%>%
mutate(Country = case_when(
"US" ~ "USA",
"CAN" ~ "Canada",
TRUE ~ ""))

Second attempt -

df <- df%>%
mutate(Country = case_when(
Country == "US" ~ "USA",
Country == "CAN" ~ "Canada"))

I get this error in both the cases

Thanks!

Your second attempt looks fine to me. Here's a reprex example:

library(dplyr)

df <- tibble(Country = c("US", "CAN", "MEX"))

df %>%
  mutate(
    Country = case_when(
      Country == "US" ~ "USA",
      Country == "CAN" ~ "Canada",
      TRUE ~ ""
      )
    )
#> # A tibble: 3 x 1
#>   Country 
#>   <chr>   
#> 1 "USA"   
#> 2 "Canada"
#> 3 ""

Created on 2020-06-15 by the reprex package (v0.3.0)

Yes, I thought so too. But I still see the same error.

Are you sure you are running the exact code in my example? Can you copy and paste the exact code and error message you get?

Yes, this is same as what you are suggesting:
df <- df%>%
mutate(Country = case_when(
Country == "US" ~ "USA",
Country == "CAN" ~ "Canada",
TRUE ~ ""
))

Your code works for me. Are you sure that your variable name in your data frame (df) is called Country? I really can't help any more unless you provide a full reproducible example. See here for how to create one:

No problem at all!
Thanks for trying! This is what I am doing as of now:

Again, your code looks fine to me. So to figure out your problem, you'll need to post a reproducible example including data. Please see the link in the previous post for more information.

I agree that the code looks fine. does the error persist over sessions?
if you Control + Shift + F10 to restart R ?

Thanks @nirgrahamuk and @mfherman
After restarting, it works magically! Thanks for working on this!

Quick question - In this case, I kept blank ("") for cases not true. However, if there were few more countries and I wanted to keep them as is (with their original names), then what would be the best way to do.

Thanks again!

Glad it's working. That's one reason it's so import to provide a reprex -- often there are stray objects in your environment that cause surprising errors. A reprex will always run in a clean environment.

If you don't want to change other rows, you can do the following which will return the original value of Country for those that don't match you case statement.

df %>%
  mutate(
    Country = case_when(
      Country == "US" ~ "USA",
      Country == "CAN" ~ "Canada",
      TRUE ~ Country
      )
    )

you would change the last expression to TRUE ~ Country (i.e. the original unadjusted content of country)

library(dplyr)

df <- tibble(Country = c("US", "CAN", "MEX"))

df %>%
  mutate(
    Country = case_when(
      Country == "US" ~ "USA",
      Country == "CAN" ~ "Canada",
      TRUE ~ Country
    )
  )

Perfect! Thank you so much! I appreciate all the help!
Also, thanks for this TRUE ~ Country. I think sooner or later I might use it too :slight_smile:

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