I have a dataframe like this:
df <- data.frame(width = c(1,2,2,4), values = c(10,11,13,20))
I want to use rollapply to compound the 'values' column, but I want to use a variable width for each row, getting the width from the appropriate row in the 'width' column.
For the life of me I can't figure out how to pass the width parameter as a variable from the 'width' column.
I have a solution using dplyr's mutate, but it's clunky and very slow. Below is my current code, with a static '3' in place of the value I'd like to sub in:
rollapply(df[ , values_column], width = 3, function(x) rolling_compounder(x),
align = "right", fill = NA, partial = FALSE, by.column = FALSE)
EDIT:
Using decimal values: 0.1, 0.11, 0.13, 0.2
And the same widths as above, I would expect the compounded results to be:
0.1, 0.221, 0.254, 0.656