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.
>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"