Reputation: 73
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.
Upvotes: 1
Views: 62
Reputation: 402
completing akrun's answer:
install.packages("parsedate")
library(parsedate)
ftse$Date<-as.Date(parse_date(ftse$Date))
Upvotes: 1
Reputation: 52399
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"
Upvotes: 2