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 a vector of year/quarters from a starting date

I have a string starting date, say, "2021 Q3" and I would like to create a vector of "Year Quarter"s following the start date. For example if the required length is 5 my output would be

"2021 Q3" "2021 Q4" "2022 Q1" "2022 Q2" "2022 Q3"

I tried to use as.Date() in base R to no avail.

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

Is there a succinct way of doing this in R?

>Solution :

Convert the input x to yearqtr class. It represents year and quarter interanlly as year + 0, 1/4, 1/2 and 3/4 so just add whatever number of quarters needed. It renders them as shown. See ?yearqtr for more info.

library(zoo)

x <- "2021 Q3"
as.yearqtr(x) + seq(0, length = 5) / 4
## [1] "2021 Q3" "2021 Q4" "2022 Q1" "2022 Q2" "2022 Q3"
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