I have a bizarre database with all sorts of date types, such as:
datfra <- data.frame(ExactDate = c(NA, NA, NA, NA, NA),
DateEstimated = c("1974", "22/12/1984","<1982","year?","23/05/1981" ))
> datfra
ExactDate DateEstimated
1 NA 1974
2 NA 22/12/1984
3 NA <1982
4 NA year?
5 NA 23/05/1981
Would I would like to achieve is to transfer correct dates (the ones fitting dd/mm/yyyy) to the column ExactDate
and leaving the unfitting ones as NA
's such as:
> desired_datfra
ExactDate DateEstimated
1 NA 1974
2 22/12/1984 22/12/1984
3 NA <1982
4 NA year?
5 23/05/1981 23/05/1981
Any help will be welcomed!!
CodePudding user response:
Using as.Date
and format
, non-matching dates automatically get discarded.
transform(datfra, ExactDate=format(as.Date(DateEstimated, '%d/%m/%Y'), '%d/%m/%Y'))
# ExactDate DateEstimated
# 1 <NA> 1974
# 2 22/12/1984 22/12/1984
# 3 <NA> <1982
# 4 <NA> year?
# 5 23/05/1981 23/05/1981
CodePudding user response:
Here is an option using dmy
from lubridate
package.
By using this function only the dates with the correct format will be parsed the other will left NA.
library(dplyr)
library(lubridate)
datfra %>%
mutate(ExactDate = dmy(DateEstimated))
ExactDate DateEstimated
1 <NA> 1974
2 1984-12-22 22/12/1984
3 <NA> <1982
4 <NA> year?