I have a input dataset that is like this:
Name | Date |
---|---|
A | 2018 Q2 |
A | 2019 Q3 |
B | 2018 Q4 |
B | 2019 Q4 |
And my desired output looks like this:
Name | Date |
---|---|
A | 2018 Q2 |
A | 2018 Q3 |
A | 2018 Q4 |
A | 2019 Q1 |
A | 2019 Q2 |
A | 2019 Q3 |
B | 2018 Q4 |
B | 2019 Q1 |
B | 2019 Q2 |
B | 2019 Q3 |
B | 2019 Q4 |
Currently I have a for-loop (parse out Year and Quarter and keep adding 1 to start quarter until I reach the end date) However, running the loop takes a lot of time. I am wondering if there are any faster ways of doing this?
Thanks!
Convert to yearqtr and then use seq
. Note that yearqtr represents year and quarters as the year plus 0, 1/4, 2/4 and 3/4 for the 4 quarters so using seq
with by=1/4 will work.
library(dplyr)
library(zoo)
DF %>%
mutate(Date = as.yearqtr(Date)) %>%
reframe(Date = seq(Date[1], Date[2], 1/4), .by = Name)
giving
Name Date
1 A 2018 Q2
2 A 2018 Q3
3 A 2018 Q4
4 A 2019 Q1
5 A 2019 Q2
6 A 2019 Q3
7 B 2018 Q4
8 B 2019 Q1
9 B 2019 Q2
10 B 2019 Q3
11 B 2019 Q4
DF <- data.frame(
Name = rep(c("A", "B"), each = 2L),
Date = c("2018 Q2", "2019 Q3", "2018 Q4", "2019 Q4")
)