-1

I got this data, but when i use mutate(acc_growth_p1 = accumulate(1+port1, '*')), I am not getting the cumulative figure.

I tried to use mutate and them the accumulate, but I keep getting only the 1+port figure.

mutate(acc_growth_p1 = accumulate(1+port1, '*'))
Phil
  • 7,287
  • 3
  • 36
  • 66
  • 3
    It's not clear what you are trying to achieve. It would be easier to help if you could provide a reproducible example, including a sample of your data, and the intended output. – Phil Sep 09 '22 at 17:30
  • I am trying to get the accumulative return of each month of port1. row 2 should be (1+port1 [t-1])*(1+port1[t]) and so on. – hr_lindomar Sep 09 '22 at 17:32
  • 1
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064) What should the value for row 1 be since there's no previous value? – MrFlick Sep 09 '22 at 17:39
  • 1
    Not able to reproduce the issue `data.frame(port1 = c(0.026826021, 0.023703176, 0.052671447, -0.028429055, 0.007275008)) %>% mutate(acc_growth_p1 = accumulate(1 + port1, `*`)) %>% pull(acc_growth_p1)# [1] 1.026826 1.051165 1.106531 1.075074 1.082895` – akrun Sep 09 '22 at 18:36

1 Answers1

1
mutate(acc_growth_p1 = cumprod(port1 + 1))

For example:

data.frame(rates = seq(0, 0.5, by = 0.1)) %>%
  mutate(growth = cumprod(rates + 1))

  rates growth
1   0.0 1.0000
2   0.1 1.1000
3   0.2 1.3200
4   0.3 1.7160
5   0.4 2.4024
6   0.5 3.6036
Jon Spring
  • 55,165
  • 4
  • 35
  • 53