I want to reduce a vector with length n (where n is even) into length n/2 using R
If the vector was (10,50,30,20,40,70), the output should be (-40,10,-30) i.e. (10-50,30-20,40-70)
I can do this in a loop like so
vec <- c(10,50,30,20,40,70)
newVec <- numeric(length(vec)/2)
sequence <- seq(1,length(vec),2)
for (i in 1:(length(vec)/2)) {
newVec[[i]] <- vec[sequence[i]]-vec[sequence[i]+1]
}
> newVec
[1] -40 10 -30
This is a pretty ugly way to do it though so I was wondering if there was anything better?