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

R: Creating a New Column with Quarterly dates in r

I have the following dataframe:

df = obs seriesA seriesB
1        50       50
2        50       50
3        50       50
4        50       50
5        50       50

I would like to create a new column called date that contains quarterly data from a given interval. e.g.:

df = obs seriesA seriesB   date
1        50       50       Q1-2000
2        50       50       Q2-2000
3        50       50       Q3-2000
4        50       50       Q4-2000
5        45       34       Q1-2001

I would like to have an easy enough method that I can extrapolate to longer data frames: e.g. from Q1-2000 to Q1-2019. Also it would have to be in date format to be able to read it as a date.

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 :

You can create a sequence using seq, and then use zoo::as.yearqtr to format the string:

library(zoo)
seq(from = as.yearqtr("2000-01-01"), to = as.yearqtr("2019-01-01"), by = 1/4) |>
  format("Q%q-%Y")

#[1] "Q1-2000" "Q2-2000" "Q3-2000" "Q4-2000" "Q1-2001" "Q2-2001" "Q3-2001" "Q4-2001" "Q1-2002"
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