roll_fun | R Documentation |
Apply rolling functions
roll_fun(x, n = 5L, FUN = mean, ..., fromLast = FALSE, keep = FALSE)
x |
a vector |
n |
size of groups |
FUN |
a function to apply as a |
... |
additional arguments passed to |
fromLast |
logical; if |
keep |
logical; if |
A vector of the same length of x
with calculations obtained by
FUN
.
cbind(1:10, roll_fun(1:10, 2, keep = TRUE))
cbind(rep(1, 10), roll_fun(rep(1, 10), 5, sum))
dat <- data.frame(x = c(1, 1, 2, 2, 2, 3, 4, 5, 5, 5),
y = 1:10)
## compare:
within(dat,
z <- unlist(by(dat, dat$x, function(ii)
roll_fun(ii$y, length(ii$y), sum))))
do.call('rbind', by(dat, dat$x, cumsum))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.