Mario M. Mario M. - 25 days ago 10
R Question

How to split in R a date range into quarterly basis period?

I want to split monthly date range into quarterly basis period.
My Date Variable is built with as.yearmonth:

> data$DATES
jul. 2014
ago. 2014
sep. 2014
...
...
jun. 2016
jul. 2016
ago. 2016
sep. 2016


How should I do it? cut function works with dates? I don't want to do this manually.

Answer

Lubridate has a quarter function. Check it out:

library(lubridate)
x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31"))
quarter(x)
quarter(x, with_year = TRUE)

Then the only thing you have to do is sort out your date formats so that they are understood.

Comments