case_when to parse multiple date formats in the wild?

I'm parsing human entered dates with a few different easily parsed formats. My solution gets along well when using if/else conditions, but how come it gives warnings with case_when? It seems like it's still evaluating all conditions and yet it still worked...

suppressPackageStartupMessages(library(dplyr))
#> Warning: package 'dplyr' was built under R version 3.6.2
library(stringr)
library(readr)
library(purrr)

dates <- c("May.14, 2017", "May20, 2017")

# (1) using if/else ---------------------
my_parse_date <- function(date){
  if(str_detect(date, "\\.")) {parse_date(date, format = "%b.%d, %Y")}
  else {parse_date(date, format = "%b%d, %Y")}
}

map(dates, ~my_parse_date(.x))
#> [[1]]
#> [1] "2017-05-14"
#> 
#> [[2]]
#> [1] "2017-05-20"

# (2) using case_when ---------------------
my_parse_date2 <- function(date){
  case_when(
    str_detect(date, "\\.") ~ parse_date(date, format = "%b.%d, %Y"),
    TRUE ~ parse_date(date, format = "%b%d, %Y"))
}

map(dates, ~my_parse_date2(.x))
#> Warning: 1 parsing failure.
#> row col           expected       actual
#>   1  -- date like %b%d, %Y May.14, 2017
#> Warning: 1 parsing failure.
#> row col            expected      actual
#>   1  -- date like %b.%d, %Y May20, 2017
#> [[1]]
#> [1] "2017-05-14"
#> 
#> [[2]]
#> [1] "2017-05-20"

case_when will execute both RHS for all inputs and then create a vector based on LHS. That is why you see warnings -- they come from the dates that couldn't be parsed correctly.

3 Likes

I guess my question is then: why does it execute both RHS? I'm wondering if it has always been doing this but returning the correct answer without warning.

Yes, it always does this. That's just how it works. Looking at the source code, it looks like core logic didn't change for at least 1.5 years, so difficult to say if you've seen different behavior before.

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