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
As they are quarterly returns divide the vector into groups of 4 and calculate for each group.
tapply(a, gl(length(a)/4, 4), function(x) prod(1 + x) - 1)
# 1 2
#0.2579 0.1800
The grouping part and calculation part can be done in variety of ways. For example, the above can also be done using split
+ sapply
sapply(split(a, gl(length(a)/4, 4)), function(x) prod(1 + x) - 1)
Or grouping can also be done using rep
tapply(a,rep(seq_along(a), each = 4, length.out = length(a)),
function(x) prod(1 + x) - 1)