I am finding hard time calculating Geometric date for each column for the specific years. I had to calculate that for multiple columns. It was done in excel and now we like to move to r for broader audience Below are the formulas used in Excel
(GEOMEAN(1+DK45:DK48)^4)^(1/4)-1
I tried applying mean.geometric formula from performance library which give exactly the result but not sure how to apply that by different quarters on whole column
TotalReturn %>%
mutate(mpgGM = rollapply(l12420, 3, geometric.mean, fill=NA,
align="left"))
My sample data set is
structure(list(Quarter = structure(c(18717, 18808, 18900, 18992,
19082, 19173, 19265, 19357, 19447, 19538, 19630, 19722), class = "Date"),
A = c(0.043, 0.044, 0.044, 0.044, 0.044, 0.046, 0.048, 0.049,
0.05, 0.05, 0.05, 0.051), B = c(-0.002, -0.001, 0.002, 0.008,
0.015, 0.02, 0.024, 0.025, 0.025, 0.023, 0.022, 0.022)), row.names = c(NA,
-12L), class = "data.frame")
This is the expected result
2021 Q4 4.06%
2022 Q4 4.68%
2023 Q4 5.04%