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:

# Generate data
dat <- data.frame(,

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_example: random walk

Rollply supports groups and 2D moving windows too:

# Generate three 2D random walks
dat <- ddply(data.frame(person=c('fran├žois','nicolas','jacques')), ~ person,

# 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='+') +

rollply_example: random walk with groups

# 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)) +

rollply_example: random walk with 2D window



r if ( !require(devtools) ) { install.packages('devtools') library(devtools) } install_github('alexgenin/rollply') library(rollply)

r install.packages("rollply", repos="")

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

Try the rollply package in your browser

Any scripts or data that you put into this service are public.

rollply documentation built on May 30, 2017, 8:07 a.m.