0

I have dates in Numeric mode like this: 20/05/2012

What's the most elegant way to extract the year and month from this?

8128
  • 27
  • 2
  • 6
  • Possibly with `lubridate`, e.g. `lubridate::month(lubridate::dmy("20/05/2012"))` and `lubridate::year(lubridate::dmy("20/05/2012"))`. – nrussell Nov 09 '15 at 14:52
  • If you need names weekdays(as.Date("20/05/2012", format = "%d/%m/%Y")) [1] "Sunday" months(as.Date("20/05/2012", format = "%d/%m/%Y")) [1] "May" – Vasile Nov 09 '15 at 15:07

1 Answers1

3

First you need to convert it to a date object:

x <- as.Date("20/05/2012", format="%d/%m/%Y")

Then to get the month number:

format(x,"%m")

And to get the year number:

format(x,"%Y")

If you want the month name (note, this is in your machine language):

months(x)
kneijenhuijs
  • 1,189
  • 1
  • 12
  • 21