Home > Back-end >  How to convert dates in format Jan 01-21 to YYYY-MM-DD in R?
How to convert dates in format Jan 01-21 to YYYY-MM-DD in R?

Time:09-26

I am working with data from excel to R. All of the other spreadsheets I imported returned in R as numbers from 1899-12-30, and I was able to convert those to the format I need using as.Date(). This file uses Jan DD-YY format, and I can't figure out how to convert it in R. Everything I have tried has returned NA values. Any ideas to help?

CodePudding user response:

Do you mean this?

> as.Date("Jan 01-21", format = "%b %d-%y")
[1] "2021-01-01"

CodePudding user response:

We could use mdy function from lubridate package:

library(lubridate)

date <- "Jan 01-21"
mdy(date)

output:

> mdy(date)
[1] "2021-01-01"

Or another option is parse_date

library(parsedate)
parse_date(date)
[1] "2021-01-21 UTC"
  •  Tags:  
  • r
  • Related