Suppose I have a data frame with the following structure:
DF <- data.frame(x = 0:4, y = 5:9) > DF xy 1 0 5 2 1 6 3 2 7 4 3 8 5 4 9
What is the most efficient way to turn DF into a data frame with the following structure:
wxy 1 0 5 1 1 6 2 1 6 2 2 7 3 2 7 3 3 8 4 3 8 4 4 9
Where w is a window with a length of 2 passing through the DF data frame. The window length must be arbitrary, i.e. 3 exit lengths
wxy 1 0 5 1 1 6 1 2 7 2 1 6 2 2 7 2 3 8 3 2 7 3 3 8 3 4 9
I am a little puzzled by this problem, because a data frame may also contain an arbitrary number of columns, i.e. w, x, y, z, etc.
/ edit 2: I realized that editing 1 is a bit unreasonable, as xts doesn't seem to be dealing with a few data point observations
r data-manipulation rolling-computation data-management
Zach
source share