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

后端 未结 5 585
难免孤独
难免孤独 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:45

    Using apply:

    apply(matrix(a, nrow = 4), 2, function(x) prod(1 + x) - 1)
    #[1] 0.2578742 0.180033
    

提交回复
热议问题