Skip to content

Instantly share code, notes, and snippets.

@ibombonato
Last active June 21, 2016 22:06
Show Gist options
  • Save ibombonato/7a785ef197892c31c82186b92ed349e2 to your computer and use it in GitHub Desktop.
Save ibombonato/7a785ef197892c31c82186b92ed349e2 to your computer and use it in GitHub Desktop.
Parsing dates with lubridate with NA for invalid ones
library(lubridate)
mother2 <- as.data.frame(list(month=c(2,2), day=c(28,30), year=c(1988, 1988)))
mother2$birth1 <- mapply(function(y, m, d){
my_data <- paste0(as.character(y),
"-", as.character(m),
"-", as.character(d))
ifelse(is.na(lubridate::ymd(my_data, quiet = TRUE)), NA, as.POSIXct(my_data, origin="1970-01-01"))
}, y = mother2$year, m = mother2$month, d = mother2$day)
#> mother2
# month day year birth1
#1 2 28 1988 573015600
#2 2 30 1988 NA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment