rollply: Rollply

Description Usage Arguments Details Value See Also Examples

View source: R/rollply.R

Description

Applies a function in a moving window then combines the results in a data.frame.

Usage

1
2
3
rollply(.data, .rollvars, wdw.size, fun, grid = NULL, grid_npts = NULL,
  grid_type = "identical", grid_opts = NULL, padding = "none",
  .parallel = FALSE, ...)

Arguments

.data

data.frame to be processed

.rollvars

a formula of the form ~ a + b + ... | c describing over which variables the window should move. a and b denote the variables used for the rolling window and c an optional grouping variable.

wdw.size

window size

fun

function to apply

grid

data.frame of points at which the computation is done. If NULL then a grid is generated using grid_npts and grid_type.

grid_npts

if grid is unspecified, the number of points of the grid to build.

grid_type

The type of grid to generate (one of "squaretile", "identical", "ahull_crop", "ahull_fill").

grid_opts

A list of named options to be passed to the grid-generating function.

padding

padding policy at the edges of the dataset, one of 'none', 'outside', 'inside', or a numeric value

.parallel

whether to use parallel processing (see ddply for more information on parallelism).

...

other arguments passed to ddply and fun

Details

rollply applies a function in a window moving over one or more variables. It is built upon ddply so it inherits many of its useful options 'such as .parallel or .progress.

Rollply uses internally a grid spanning the coordinates specified in the formula. For each point of this grid, it then selects the corresponding subset of the data frame within wdw.size, and pass it to the function fun. The return values of fun are then combined into a data frame.

If specified, the grid should have column names matching the ones provided in the left-hand side of the formula. If the grid is unspecified, then rollply automatically computes an appropriate grid depending on the option grid_type:

Each of these grid types correspond to a function with prefix build_grid. Some of them have options that can be passed by providing a named list as argument grid_opts.

The padding policy indicates what to do at the edges of the dataset. A value of "inside" indicates that no value will be computed when the window is not entirely contained within the range of the dataset. This parameter does not apply for ahull-based grids.

Value

A data.frame with the function results at each grid point. Make sure the function results are fit for merging into a data.frame (i.e. that they can be merged using rbind.fill).

See Also

build_grid_identical, build_grid_squaretile, build_grid_ahull_crop, build_grid_ahull_fill

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# see also vignette("rollply") for a visual introduction
library(plyr)

# 1D example: make a trendline for a time-series
dat <- data.frame(time     = seq.int(1000),
                  position = cumsum(rnorm(1000,0,10)))

rollav <- rollply(dat, ~ time, wdw.size=10,
                  summarise, position=mean(position))

plot(position ~ time, data = dat, pch = 20)
lines(rollav, col = 'red', lwd = 3)

# 2D example

# Generate three 2D random walks
dat <- ddply(data.frame(person = c('francois','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_res = 1000,
                  summarise, x = mean(x), y = mean(y))

if ( require(ggplot2) ) {
  ggplot(dat, aes(x, y, color = person)) +
    geom_point(alpha = .5, shape = '+') +
    geom_path(data = rollav)
}

# Where did people spend their time ?
# we pregenerate the grid to fix it across groups
fixed_grid <- build_grid_squaretile(dat[ ,c('x','y')], 2000) 
rollav <- rollply(dat, ~ x + y | person, wdw.size = 2, grid = fixed_grid,
                  summarise, time.spent = length(time))

if ( require(ggplot2) ) {
  ggplot(subset(rollav, time.spent > 0)) +
    geom_point(aes(x, y, color = person, size = time.spent), alpha = .5) +
    facet_grid(~person)
}

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