CASE_When error

trying to run the following code based on my data set, I have to set factors based on specific dates in my data set

MergedData %>%

  • mutate(new_var = case_when(date ~ "2008-09-15" ~ "Lehman.Bankruptcy",
  •                          date ~ "2020-03-11" ~ "Pandemic",
    
  •                          TRUE ~ 'BAU')) 
    

and I end up with this error and im super stuck
Error in mutate():
! Problem while computing new_var = case_when(...).
Caused by error in case_when():
! LHS of case 1 (date ~ "2008-09-15") must be a logical vector, not a formula object.
Backtrace:

  1. MergedData %>% ...
  2. dplyr::case_when(...)

If you are doing a comparison use ==, the tilde is only for assigning the casewhen results

sorry could you re word that im a bit confused

the code is

library(dplyr)

MergedData %>%
mutate(new_var = case_when(date ~ "2008-09-15" ~ "Lehman.Bankruptcy",
date ~ "2020-03-11" ~ "Pandemic",
TRUE ~ 'BAU'))

im confused what is wrong

library(dplyr)

MergedData %>%
mutate(new_var = case_when(date == "2008-09-15" ~ "Lehman.Bankruptcy",
date == "2020-03-11" ~ "Pandemic",
TRUE ~ 'BAU'))
1 Like

This topic was automatically closed 21 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.