Calculate Returns over Period of Time

前端 未结 6 622
[愿得一人]
[愿得一人] 2021-02-04 09:51

I\'m trying to get a time series of returns for holding a certain asset for a specific time.

My dataframe looks like this:

Date          Price
1998-01-01         


        
6条回答
  •  灰色年华
    2021-02-04 10:33

    You can just use offset indices by subtracting from a range. (.... but remember that R does not use 0 as a valid index.) Let's say your prices are the second column in a dataframe named prcs2 the first three returns with an interval of 19 days with your data would be :

    prcs2[ (20:22)-19, 2] <-c(20,22,21)
    prcs2[ (20:22), 2] <-c(25,19,20)
    log(prcs2[20:22, 2]/prcs2[ (20:22)-19, 2])
    #[1]  0.22314355 -0.14660347 -0.04879016
    

提交回复
热议问题