Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create Quarter from %Y-%m-%d in R?

I have Time in %Y-%m-%d format, how can I create Quarter to get:

Time       | Quarter
2018-01-31 | 2018 Q1
2018-02-28 | 2018 Q1
2018-03-31 | 2018 Q1
2018-04-30 | 2018 Q2
2018-05-31 | 2018 Q2
2018-06-30 | 2018 Q2

Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We could achieve this by using lubridates ymd and year function combined with base Rs quarters function:

library(lubridate)
library(dplyr)
df %>% 
  mutate(Time = ymd(Time),
         Quarter = paste(year(Time), quarters(Time)))
        Time Quarter
1 2018-01-31 2018 Q1
2 2018-02-28 2018 Q1
3 2018-03-31 2018 Q1
4 2018-04-30 2018 Q2
5 2018-05-31 2018 Q2
6 2018-06-30 2018 Q2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading