-1

in the following example, as.Date is unable to parse dates of 30th and 31st of each month.

dates <- c("28/03/2020", "29/03/2020","30/03/2020", "31/03/2020",
           "28/04/2020", "29/04/2020", "30/04/2020",
           "28/05/2020", "29/05/2020", "30/05/2020", "31/05/2020")
as.Date(dates, format = "%d/%M/%Y")

The result I am getting is:

[1] "2020-02-28" "2020-02-29" NA           NA           "2020-02-28" "2020-02-29"
[7] NA           "2020-02-28" "2020-02-29" NA           NA  

I´m running R Studio Version 1.4.1103 on a Windows machine.

  • You can also use `lubridate::dmy(dates)` See https://stackoverflow.com/questions/38146160/how-to-convert-dd-mm-yy-to-yyyy-mm-dd-in-r and https://stackoverflow.com/questions/8398131/convert-string-to-date-format-dd-mm-yyyy – Ronak Shah Feb 21 '21 at 00:41

1 Answers1

3

You'll need a small m in your format argument.

dates <- c("28/03/2020", "29/03/2020","30/03/2020", "31/03/2020",
           "28/04/2020", "29/04/2020", "30/04/2020",
           "28/05/2020", "29/05/2020", "30/05/2020", "31/05/2020")
> as.Date(dates, format = "%d/%m/%Y")
 [1] "2020-03-28" "2020-03-29" "2020-03-30" "2020-03-31" "2020-04-28" "2020-04-29" "2020-04-30" "2020-05-28" "2020-05-29" "2020-05-30"
[11] "2020-05-31"
tester
  • 1,662
  • 1
  • 10
  • 16