0

In R, I have a couple of tibbles with many rows (4 million or so). I run a loop through one of the tibbles to construct cumulative series based on the other tibble.

for (i in 1:nrow(PortfolioDevt)){
  if (PortfolioDevt$Period[i] == 0){
    PortfolioDevt$Assets[i] <- 100
  } else {
    PortfolioDevt$Assets[i] <- PortfolioDevt$Assets[i-1] * (1 + css$Return[i])
  }
}

However, I am really disappointed with the speed of this.

Am I making some beginner's mistake here? Is there a much faster way to do this?

PS: There is nothing wrong with the results, which are as expected, except for running time. :(

Řídící
  • 248
  • 1
  • 9
  • If you can provide a small sample of your input(s) and expected output, it's much more likely that somebody can advise you. – r2evans Nov 03 '22 at 22:05
  • Provide a sample dataframe, and the expected results – Onyambu Nov 03 '22 at 22:10
  • 5
    Without seeing the data, this sounds suspiciously like a compound interest calculation, which you could do much faster - e.g.: https://stackoverflow.com/questions/50593439/calculating-compound-interest-with-vector-of-rates/50593687 – thelatemail Nov 03 '22 at 22:38
  • @thelatemail Thank you so much for this. Using cumprod - new to me - speeds things up tremendously! – Řídící Nov 04 '22 at 07:50

0 Answers0