Suppose I have a vector x with n elements. I want to use any vectorised function, say cumprod, on every alternate number of x, i.e. every 1, 3, 5 and so on and another on 2, 4, 6 and so on. I am adding a reprex and code tried. The code works, but it seems I am unnecessarily taking a long route and code can be shortened. Can it be?
x <- 5:14
cumprod((x * (seq_along(x) %% 2)) + (seq_along(x)-1) %% 2) * seq_along(x) %% 2 +
cumprod((x * ((seq_along(x)-1) %% 2)) + seq_along(x) %% 2) * (seq_along(x)-1) %% 2
#> [1] 5 6 35 48 315 480 3465 5760 45045 80640
Here cumprod is just an example function. I may have to use other functions in alternate sequence as well.