Asteroid098
Asteroid098

Reputation: 2825

How to format dates in R

I'm having trouble changing date format in R. I have a vector "StartDate" with dates and time for instance in the format:

01Feb1991 00:00

I did:

as.POSIXct(as.character(bio$StartDate), format = "%d/%m/%Y %H:%M")

...but I got NAs as a result. Would there be a different way to change the vector into date format?

Upvotes: 0

Views: 93

Answers (1)

mathematical.coffee
mathematical.coffee

Reputation: 56935

The format you provide has to match your string. In your case, that's '%d%b%Y %H:%M' (you don't have slashes between day, month and year, and your month is the abbreviated name, not the number).

as.POSIXct('01Feb1991 00:00', format='%d%b%Y %H:%M')

See ?strptime (mentioned in ?as.POSIXct) for various tokens you can use for dates.

Upvotes: 2

Related Questions