Suppose I have the following data:
a<- c(1:10)
b<- c(10:1)
Now I want to make a Consecutive calculation (of variable length in this example 2)on both rows (a and b) and save the output in two separate lists(a and b).
The calculation should look like the following:
for a:
(1+2)/2; (2+3)/2; (3+4)/2;...; (9+10)/2
for b(the same):
(10+9)/2; (9+8)/2; (8+7)/2;...;(2+1)/2
a
1,5 2,5 3,5 ... 9,5
b
9,5 8,5 7,5 ... 1,5
I found this function in StackOverflow:
v <- c(1, 2, 3, 10, 20, 30)
grp <- 3
res <- sapply(1:(length(v)-grp+1),function(x){sum(v[x:(x+grp-1)])})
Which pretty much does what i need but I would prefer a function which does that without using sapply
and just base R.
Any Help would be appreciated!