Gary Cooper
Gary Cooper

Reputation: 11

Simplifying multiple if else inside mutate for the same variable

I am trying to clean up some data that is in the expr column. This is just a subset but there are many more rules that I have implemented. However, the method I am using shown below is very slow.

Is there any way to simplify this and make it more efficient?

Thanks

df %>%
    mutate(expr = ifelse(entity == "Frequency",gsub( "twice daily", "2", expr), expr)) %>%
    mutate(expr = ifelse(entity == "Frequency",gsub( "twice a day", "2", expr), expr)) %>%
    mutate(expr = ifelse(entity == "Frequency",gsub( "once daily", "1", expr), expr)) %>%
    mutate(expr = ifelse(entity == "Frequency",gsub( "daily", "1", expr), expr))

Upvotes: 0

Views: 49

Answers (1)

mhovd
mhovd

Reputation: 4097

This is where case_when from dplyr absolutely shines, as it practically allows you to chain multiple if-statements.

The syntax is case_when(logical ~ action). If the logical statement is true, the action will be performed.

df %>% 
  mutate(expr = case_when(
    entity == "Frequency" ~ gsub("twice (daily|a day)", "2", expr),
    entity == "Frequency" ~ gsub( "once daily", "1", expr),
    entity == "Frequency" ~ gsub( "daily", "1", expr)
  ))

Upvotes: 1

Related Questions