I am familiar with R since I have been using it for several years. Unfortunately, I'm not very good at creating functions that include looping or repeating an equation. The problem is this:
I have a vector containing more than 1000 values. I would like to calculate the absolute difference between two comparable means of equal size from a subset of this vector.
Here is an example.
I have a vector (vec) of length 8
[1] 0.12472963 1.15341289 -1.09662288 -0.73241639 0.06437658 -0.13647136 -1.52592048 1.46450084
I would like to calculate the average of the first two values ββ(0.12472963, 1.15341289) and get the absolute difference with the average of the following values ββ(-1.09662288 -0.73241639), after which, working along the way down the vector.
In this case, I can easily use the following equation:
abs(mean(vec[1:2])-mean(vec[3:4]))
and gradually increase each number by 1 to manually work until the end of the vector. I would get the following vector.
[1] 1.553591 0.3624149 0.8784722 0.497176 0.005337574
However, I would like to have an automatic procedure that allows me to do this over long vectors and change the number of values ββfrom which the means can be calculated.
It seems to me that this should be relatively simple, but I do not know where to start.
source share