0

I am trying to find the Yield to Date for a monthly raw data with lag function. Is there a better way to code it instead of the one here? After this I would need to find the YTD for daily data and I believe with the code I have, it will take ages, hence I would need advice on a better way to code it. Thx.

YTD<- (lag(Data2,0) + lag(Data2,1) + lag(Data2,2) + lag(Data2,3) +
       lag(Data2,4) + lag(Data2,5) + lag(Data2,6) + lag(Data2,7) +
       lag(Data2,8) + lag(Data2,9) + lag(Data2,10) + lag(Data2,11) +
       lag(Data2,12))
neilfws
  • 32,751
  • 5
  • 50
  • 63
kaix
  • 305
  • 3
  • 10
  • 3
    Welcome to Stack Overflow. Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing some or all of `Data2` in a plain text format, and explaining where the `lag` function comes from. – neilfws Nov 05 '20 at 03:29
  • This is sounding suspiciously like a cumulative sum - see `?cumsum` – thelatemail Nov 05 '20 at 03:46

0 Answers0