I am quite familiar with R as I've been using it for a few years now. Unfortunately, I am not very well versed in creating functions that involve looping or repetition of an equation. The problem goes as follows:
I have a vector containing over 1000 values. I would like to calculate the absolute difference between two juxtaposing means of equal size from a subset of that vector.
Here is an an example.
I have the 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 calculate the mean of the first 2 values ( 0.12472963, 1.15341289) and obtain the absolute difference with the mean of the following 2 values (-1.09662288 -0.73241639), thereafter, working my way down the vector.
In this case, I can easily use the following equation:
abs(mean(vec[1:2])-mean(vec[3:4]))
and incrementally increase each number by 1 so as to work my way down manually until the end of the vector. I would obtain the following vector.
[1] 1.553591 0.3624149 0.8784722 0.497176 0.005337574
What I would like, however, to have an automated routine which enables be me to do that over long vectors and change the number of values from which to calculate the means.
It appears to me that it should be relatively simple, but I do not know where to start.