Cumulative Returns with NA's in R
问题 I have the following data frame: df <- data.frame(Return1=c(NA, NA, .03, .04, .05), Return2=c(.25, .33, NA, .045, .90), Return3=c(.04, .073, .08, .04, .01)) Return1 Return2 Return3 1 NA 0.250 0.040 2 NA 0.330 0.073 3 0.03 NA 0.080 4 0.04 0.045 0.040 5 0.05 0.900 0.010 I would like to compute the cumulative returns, but there are missing values in the dataframe. I used: cumprod(df+1)-1 Getting as a result Return1 Return2 Return3 1 NA 0.2500 0.0400000 2 NA 0.6625 0.1159200 3 NA NA 0.2051936 4