Description Usage Arguments Details Value See Also Examples
Applies a function in a moving window then combines the results in a data.frame.
1 2 3 |
.data |
|
.rollvars |
a formula of the form |
wdw.size |
window size |
fun |
function to apply |
grid |
data.frame of points at which the computation is done. If
|
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 |
... |
other arguments passed to |
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
:
"identical" Creates a grid with an identical number of points on each dimension.
"squaretile" Creates a grid with square tiles (points are equidistant on all dimensions)
"ahull_crop" Creates a grid with square tiles, then crop the result to the alpha hull of the set of points
"ahull_fill" Same as above, but iteratively tries to return a grid
with a number of points similar to what was requests using
grid_npts
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.
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
).
build_grid_identical, build_grid_squaretile, build_grid_ahull_crop, build_grid_ahull_fill
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)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.