Hello,
The following strategy works with your data.
It uses the fact that most dates are in one of 3 formats, dmy, mdy, ymd.
It tries those formats one by one, after each try looks for NA's in the
new column.
# first round, format is dmy
DFX$dnew <- lubridate::dmy(DFX$ddate)
na <- is.na(DFX$dnew)
# second round, format is mdy
DFX$dnew[na] <- lubridate::mdy(DFX$ddate[na])
na <- is.na(DFX$dnew)
# last round, format is ymd
DFX$dnew[na] <- lubridate::ymd(DFX$ddate[na])
# remove what didn't fit any format
DFX <- DFX[!is.na(DFX$dnew), ]
DFX
Hope this helps,
Rui Barradas
Às 22:58 de 20/01/20, Val escreveu:
Hi All,
I have a data frame where one column is a mixed date format,
a date in the form "%m-%d-%y" and "%m/%d/%Y", also some are not in date format.
Is there a way to delete the rows that contain non-dates and
standardize the dates in one date format like %m-%d-%Y?
Please see my sample data and desired output
DFX<-read.table(text="name ddate
A 19-10-02
B 22-11-20
C 19-01-15
D 11/19/2006
F 9/9/2011
G 12/29/2010
H DEX",header=TRUE)
Desired output
name ddate
A 19-10-2002
B 22-11-2020
C 19-01-2015
D 11-19-2006
F 09-09-2011
G 12-29-2010
Thank you
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.