R: create a data frame out of a rolling window
问题 Lets say I have a data frame with the following structure: DF <- data.frame(x = 0:4, y = 5:9) > DF x y 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: w x y 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 length 2 window rolling through the dataframe 'DF.' The length of the window should be arbitrary, i.e a length of 3 yields w x y 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 bit stumped by