Description Usage Arguments Value Cumulative logical functions Examples
dplyr provides cumall()
, cumany()
, and cummean()
to complete R's set
of cumulative functions.
1 2 3 4 5 |
x |
For |
A vector the same length as x
.
These are particularly useful in conjunction with filter()
:
cumall(x)
: all cases until the first FALSE
.
cumall(!x)
: all cases until the first TRUE
.
cumany(x)
: all cases after the first TRUE
.
cumany(!x)
: all cases after the first FALSE
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # `cummean()` returns a numeric/integer vector of the same length
# as the input vector.
x <- c(1, 3, 5, 2, 2)
cummean(x)
cumsum(x) / seq_along(x)
# `cumall()` and `cumany()` return logicals
cumall(x < 5)
cumany(x == 3)
# `cumall()` vs. `cumany()`
df <- data.frame(
date = as.Date("2020-01-01") + 0:6,
balance = c(100, 50, 25, -25, -50, 30, 120)
)
# all rows after first overdraft
df %>% filter(cumany(balance < 0))
# all rows until first overdraft
df %>% filter(cumall(!(balance < 0)))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.