Home > OS >  Changing date format returns NA
Changing date format returns NA

Time:08-24

I have a dataset with the Date column as:

Date
-------------
Aug. 21, 2022
Aug. 19, 2022
Aug. 18, 2022
...
Aug. 22, 2017

And I need the format to be in typical Date syntax i.e %d-%m-%y. However when I run the code

ftse$Date <- as.Date('Aug. 21, 2022', format = '%b-%d-%y')
ftse$Date <- format(ftse$Date, '%d-%m-%Y')

I get a column of NAs. My guess would be that the mix of a hyphen and full stop don't agree with the format function. Does anyone have any idea how to change?

Many thanks in advance.

CodePudding user response:

Your format is slightly different than the one you indicate. Since you have a dot and a comma, you have to include them in the format string. As mentioned in the comments, you also need to set %Y (4-digit year) instead of %y (2-digit year).

as.Date('Aug. 21, 2022', format = '%b. %d, %Y')
#[1] "2022-08-21"

format(as.Date('Aug. 21, 2022', format = '%b. %d, %Y'), '%d-%m-%Y')
#[1] "21-08-2022"

CodePudding user response:

We could use parse_date

library(parsedate)
as.Date(parse_date("Aug. 21, 2022"))
[1] "2022-08-21"
  • Related