I have the following data frame:
tibble(
company = rep(x = "google", each = 15),
date = c(
"2019q1",
"2019q2",
"2019q3",
"2019q4",
"2019end",
"2020q1",
"2020q2",
"2020q3",
"2020q4",
"2020end",
"2021q1",
"2021q2",
"2021q3",
"2021q4",
"2021end"
),
values = c(1:15)
)
How can i apply the following calculation to this data frame:
This will yield a new column called new_value
e.g. for the first row of 2019q1 this would be:
new_value = 2019q1 + 2018end + 2018q1 More detailed result post 2019 years to follow
I am aware 2018 values do not exist so this will be simply NA
but when you get to 2020q1 the calculation would be this:
2020q1 + 2019end + 2019q1
2020q2 + 2019end + 2019q2
2020q3 + 2019end + 2019q3
2020q4 + 2019end + 2019q4
2020end + 2019end + 2020end
And so on and so forth.
In essence the formula per row is the following:
current date + end date value of previous year + previous date value
I have had some issues with trying to do this in long format, pivotting the data doesn't yield the desired result either