Moving-Window Add-on for 'plyr'

Apply a function in a moving window, then combine the results in a data frame.


Rollply is an R package built on top on plyr's **ply functions to make moving-window based computations easier. Just provide a function that works on a data.frame, rollply will take care of building the appropriate subsets and apply that function to each of them.

Some packages out there contain functions that compute moving-window statistics (e.g. TTR::SMA), but were not flexible enough for my use and required specific time-series classes. Rollply works on generic data.frames and builds upon plyr's advantages (parallelism, progress report, etc.).

In short, it allows writing something like this:

library(ggplot2)
library(plyr)
library(rollply)
dat <- data.frame(time=seq.int(1000),
                  position=cumsum(rnorm(1000,0,10)))
 
rollav <- rollply(dat, ~ time, wdw.size=10,
                  summarise, position=mean(position))
 
ggplot(NULL,aes(time,position)) +
  geom_point(data=dat) +
  geom_line(color='red', data=rollav)

Rollply supports groups and 2D moving windows too:

# Generate three 2D random walks
dat <- ddply(data.frame(person=c('françois','nicolas','jacques')), ~ person,
             summarise,
              time=seq.int(1000),
              x=cumsum(rnorm(1000,0,1)),
              y=cumsum(rnorm(1000,0,1)))
 
# Smoothed trajectory over ten time-steps
rollav <- rollply(dat, ~ time | person, wdw.size=10, grid_npts=1000,
                  summarise, x=mean(x), y=mean(y))
 
ggplot(dat,aes(x,y,color=person)) +
  geom_point(alpha=.5, shape='+') +
  geom_path(data=rollav)

# Where did people spend their time ?
# we fix the grid across groups
fixed_grid <- build_grid_squaretile(dat[ ,c('x','y')], 5000)
rollav <- rollply(dat, ~ x + y | person, wdw.size=2, grid=fixed_grid,
                  summarise, time.spent=length(time))
 
ggplot(subset(rollav, time.spent>0)) +
  geom_point(aes(x,y, color=person, size=time.spent)) +
  facet_grid(~person)

  • From CRAN (recommended, although there are some issues with the documentation):
  install.packages('rollply')
  • Development version from github (recommended, updated more frequently):
 if ( !require(devtools) ) {
   install.packages('devtools')
   library(devtools)
 }
 install_github('alexgenin/rollply')
 library(rollply)
  • Development version from R-forge:
 install.packages("rollply", repos="http://R-Forge.R-project.org")

Rollply is still under heavy development so please be patient in case of breakage !

News

Version 0.5.0

  • Many improvements to documentation
  • Added vignette into the package: vignette('rollply')
  • Fixed some issues regarding the creation of grids
  • Wrote a c++ equivalent to alphahull::inahull for quicker building of grids based on alpha hull

Version 0.4.2

  • Initial public release

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("rollply")

0.5.0 by Alexandre Genin, 2 years ago


http://alex.lecairn.org/rollply.html, https://github.com/alexgenin/rollply


Report a bug at https://github.com/alexgenin/rollply


Browse source code at https://github.com/cran/rollply


Authors: "Alexandre Genin <alex@lecairn.org> [aut, cre]"


Documentation:   PDF Manual  


GPL (>= 2) license


Imports plyr, Rcpp, stringr

Suggests testthat, knitr, proj4, ggplot2, rgdal, tidyr, alphahull, rmarkdown

Linking to Rcpp


See at CRAN