0

how do you work with a column of mixed date types, for example 8/2/2020,2/7/2020, and all are reflecting February, I have tried zoo::as.Date(mixeddatescolumn,"%d/%m/%Y").The first one is right but the second is wrong.

i have tried solutions here too Fixing mixed date formats in data frame? but the questions seems different from what i am handling.

Logica
  • 977
  • 4
  • 16
user322203
  • 101
  • 7

2 Answers2

2

It is really tricky to know even for a human if dates like '8/2/2020' is 8th February or 2nd August. However, we can leverage the fact that you know all these dates are in February and remove the "2" part of the date which represents the month and arrange the date in one standard format and then convert the date to an actual Date object.

x <- c('8/2/2020','2/7/2020')
lubridate::mdy(paste0('2/', sub('2/', '', x, fixed = TRUE)))
#[1] "2020-02-08" "2020-02-07"

Or same in base R :

as.Date(paste0('2/', sub('2/', '', x, fixed = TRUE)), "%m/%d/%Y")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

Since we know that every month is in February search for /2/ or /02/ and if found the middle number is the month; otherwise, the first number is the month. In either case set the format appropriately and use as.Date. No packages are used.

dates <- c("8/2/2020", "2/7/2020", "2/28/2000", "28/2/2000") # test data

as.Date(dates, ifelse(grepl("/0?2/", dates), "%d/%m/%Y", "%m/%d/%Y"))
## [1] "2020-02-08" "2020-02-07" "2000-02-28" "2000-02-28"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341