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"