0

I am trying to compute cumulative returns in R, using cumprod() for $1 invested

I seem to be getting NA values after using the cumprod() function, because the first return I'm trying to use is NA and therefore not successfully cumulating returns.

[1]           NA -0.059898142 -0.267314770 -0.075349437  0.008658063 -0.008658063  0.000000000

The first row is NA and because of that, the cumprod(x+1) function turns into all NAs

How do I remove the first row/ignore the NA?

Any input would be appreciated

camille
  • 16,432
  • 18
  • 38
  • 60
  • You can remove the first value from vector `x[-1]` and remove the first row by `x[-1, ]` – Ronak Shah Sep 28 '20 at 12:31
  • Does this answer your question? [Cumulative Returns with NA's in R](https://stackoverflow.com/questions/25574199/cumulative-returns-with-nas-in-r) – camille Sep 28 '20 at 15:02

1 Answers1

0

You can use na.omit to remove NA values in x before applying cumprod, e.g.,

cumprod(na.omit(x))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81