roll_sum {timeplyr} | R Documentation |
Fast by-group rolling functions
Description
An efficient method for rolling sum, mean and growth rate for many groups.
Usage
roll_sum(
x,
window = Inf,
g = NULL,
partial = TRUE,
weights = NULL,
na.rm = TRUE,
...
)
roll_mean(
x,
window = Inf,
g = NULL,
partial = TRUE,
weights = NULL,
na.rm = TRUE,
...
)
roll_geometric_mean(
x,
window = Inf,
g = NULL,
partial = TRUE,
weights = NULL,
na.rm = TRUE,
...
)
roll_harmonic_mean(
x,
window = Inf,
g = NULL,
partial = TRUE,
weights = NULL,
na.rm = TRUE,
...
)
roll_growth_rate(
x,
window = Inf,
g = NULL,
partial = TRUE,
na.rm = FALSE,
log = FALSE,
inf_fill = NULL
)
Arguments
x |
Numeric vector, data frame, or list. |
window |
Rolling window size, default is |
g |
Grouping object passed directly to |
partial |
Should calculations be done using partial windows?
Default is |
weights |
Importance weights. Must be the same length as x. Currently, no normalisation of weights occurs. |
na.rm |
Should missing values be removed for the calculation?
The default is |
... |
Additional arguments passed to |
log |
For |
inf_fill |
For |
Details
roll_sum
and roll_mean
support parallel computations when
x
is a data frame of multiple columns.
roll_geometric_mean
and roll_harmonic_mean
are convenience functions that
utilise roll_mean
.
roll_growth_rate
calculates the rate of percentage
change per unit time on a rolling basis.
Value
A numeric vector the same length as x
when x
is a vector,
or a list when x
is a data.frame
.
See Also
Examples
library(timeplyr)
x <- 1:10
roll_sum(x) # Simple rolling total
roll_mean(x) # Simple moving average
roll_sum(x, window = 3)
roll_mean(x, window = 3)
roll_sum(x, window = 3, partial = FALSE)
roll_mean(x, window = 3, partial = FALSE)
# Plot of expected value of 'coin toss' over many flips
set.seed(42)
x <- sample(c(1, 0), 10^3, replace = TRUE)
ev <- roll_mean(x)
plot(ev)
abline(h = 0.5, lty = 2)
all.equal(roll_sum(iris$Sepal.Length, g = iris$Species),
ave(iris$Sepal.Length, iris$Species, FUN = cumsum))
# The below is run using parallel computations where applicable
roll_sum(iris[, 1:4], window = 7, g = iris$Species)
library(data.table)
library(bench)
df <- data.table(g = sample.int(10^4, 10^5, TRUE),
x = rnorm(10^5))
mark(e1 = df[, mean := frollmean(x, n = 7,
align = "right", na.rm = FALSE), by = "g"]$mean,
e2 = df[, mean := roll_mean(x, window = 7, g = get("g"),
partial = FALSE, na.rm = FALSE)]$mean)