Fire
Fire

Reputation: 311

How do I rearrange dates in R?

Here is just one date that I have (out of more than 6,000)

02/01/15

This is expressed as January 2nd, 2015. And I would like the date to instead look like the following,

2015/01/02

I read up on this thread: Changing date format to "%d/%m/%Y"

But unless my R does not work properly, none of the answers give the correct format, instead I get this output,

0002/01/15

Upvotes: 0

Views: 69

Answers (2)

Tony Ladson
Tony Ladson

Reputation: 3649

The lubridate package is very useful for date and time manipulation

library(lubridate)
x <- lubridate::dmy('02/01/15')
format(x, format = ('%Y/%m/%d'))

Upvotes: 0

tmfmnk
tmfmnk

Reputation: 39858

You can do:

format(as.Date("02/01/15", format = "%d/%m/%y"), "%Y/%m/%d")

[1] "2015/01/02"

Upvotes: 1

Related Questions