Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 this problem, because the data frame can also contain an arbitrary number of columns, i.e. w,x,y,z etc.

/edit 2: I've realized edit 1 is a bit unreasonable, as xts doesn't seem to deal with multiple observations per data point

like image 562
Zach Avatar asked Apr 04 '11 19:04

Zach


1 Answers

My approach would be to use the embed function. The first thing to do is to create a rolling sequence of indices into a vector. Take a data-frame:

df <- data.frame(x = 0:4, y = 5:9)

nr <- nrow(df)
w <- 3            # window size
i <- 1:nr         # indices of the rows
iw <- embed(i,w)[, w:1]   # matrix of rolling-window indices of length w

> iw
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    3    4
[3,]    3    4    5

wnum <- rep(1:nrow(iw),each=w)   # window number
inds <- i[c(t(iw))]              # the indices flattened, to use below

dfw <- sapply(df, '[', inds)
dfw <- transform(data.frame(dfw), w = wnum)

> dfw
  x y w
1 0 5 1
2 1 6 1
3 2 7 1
4 1 6 2
5 2 7 2
6 3 8 2
7 2 7 3
8 3 8 3
9 4 9 3
like image 100
Prasad Chalasani Avatar answered Oct 12 '22 23:10

Prasad Chalasani