-1

I have data like

date         price 
26-12-2015   112
25-12-2015   115
24-12-2015   119
23-12-2015   NA
22-12-2015   120

I want to calculate daily returns so the syntax using ttr package is

ROC(data$price, type="discrete")

The calculation will be (112-115)-1 and so on but it will show NA for date 23-12-2015.

I want for when NA is present for previous date it should take price of the day before. I don't want to delete that row as I have dataframe with many other prices and I will lose that information.

Tunaki
  • 132,869
  • 46
  • 340
  • 423

1 Answers1

0

If we need to replace the NA by the price of the previous row, na.locf from zoo can be used (assuming that the dataset is orderered by 'date').

library(zoo)
df1$price <- na.locf(df1$price)
akrun
  • 874,273
  • 37
  • 540
  • 662