Skip to content

Instantly share code, notes, and snippets.

@jimhester
Last active January 2, 2020 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimhester/63d35ceebcbacdc272f17660b0c7dfff to your computer and use it in GitHub Desktop.
Save jimhester/63d35ceebcbacdc272f17660b0c7dfff to your computer and use it in GitHub Desktop.
Parsing dates with readr
library(readr)
the_dates <- c("2020-02-04", "04 February 2020", "2/4/20" , "4/2/20" , "04 Février 2020")
the_formats <- c("%Y-%m-%d", "%d %B %Y", "%m/%d/%y", "%d/%m/%y", "%d %B %Y")
the_locales <- list(locale("en"), locale("en"), locale("en"), locale("en"), locale("fr"))
purrr::pmap(
list(the_dates, the_formats, the_locales),
function(date, format, locale) {
parse_date(date, format = format, locale = locale)
}
)
#> [[1]]
#> [1] "2020-02-04"
#>
#> [[2]]
#> [1] "2020-02-04"
#>
#> [[3]]
#> [1] "2020-02-04"
#>
#> [[4]]
#> [1] "2020-02-04"
#>
#> [[5]]
#> [1] "2020-02-04"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment