How to apply a function to every consecutive n elements in a vector

后端 未结 5 599
难免孤独
难免孤独 2021-01-22 09:48

I am trying to convert quarterly returns to yearly returns. Given a vector of quarterly returns, how can this be done?

I am fairly new to R-programming, so I haven\'t r

5条回答
  •  情深已故
    2021-01-22 10:33

    An option:

        c(by(a, rep(1:(length(a)/4), each = 4), FUN = function(x) 
    
      (1 + x[1]) * (1 + x[2]) * (1 + x[3]) * (1 + x[4])-1
    
      )
    ))
    
            1         2 
    0.2578742 0.1800339 
    

提交回复
热议问题