Possible Duplicate:
Can `ddply` (or similar) do a sliding window?
Is there a function like rollapply (standard R or within a CRAN package) that operates on a data.frame, but doesn't convert it to a matrix. rollapply can be used with a data.frame, but if the data.frame has mixed types then each window of data is converted to a character (matrix).
I prefer a function that supports width, na.pad, align, etc. just like rollapply
Example
Take any data.frame with mixed-types
test = data.frame( Name = c( "bob" , "jane" , "joe" ) , Points = c( 4 , 9 , 1 ) )
Lets say you want to roll with window size 2. The first iteration of FUN is called with a data.frame that only includes rows 1 and 2 of test.
So RollapplyThatRespectsDataFrame( ... , FUN = function( x ) { ... } )
upon the first iteration would set x = data.frame( Name = c( "bob" , "jane" ) , Points = c( 4 , 9 ) )
The second iteration is a data.frame with rows 2 and 3 of test
.
Basically this new function does the same thing as rollapply, except it works properly with data.frames. It doesn't convert to matrix.
Try this:
Regarding the example that was added to the question after some discussion, such functionality can be layered on top of
rollapply
by applying it to the row indexes:and here it is wrapped up a bit better: