time_elapsed | R Documentation |
Calculate how much time has passed
on a rolling or cumulative basis.
time_elapsed(
x,
timespan = granularity(x),
g = NULL,
rolling = TRUE,
fill = NA,
na_skip = TRUE
)
x |
Time vector. |
timespan |
timespan. |
g |
Object to be used for grouping |
rolling |
If |
fill |
When |
na_skip |
Should |
time_elapsed()
is quite efficient when there are many groups,
especially if your data is sorted in order of those groups.
In the case that g
is supplied, it is most efficient when your data is
sorted by g
.
When na_skip
is TRUE
and rolling
is also TRUE
, NA
values are simply
skipped and hence the time differences between the current value and the
previous non-NA value are calculated. For example,
c(3, 4, 6, NA, NA, 9)
becomes c(NA, 1, 2, NA, NA, 3)
.
When na_skip
is TRUE
and rolling
is FALSE
, time differences between
the current value and the first non-NA value of the series are calculated.
For example,
c(NA, NA, 3, 4, 6, NA, 8)
becomes c(NA, NA, 0, 1, 3, NA, 5)
.
A numeric vector the same length as x
.
library(timeplyr)
library(dplyr)
library(lubridate)
x <- time_seq(today(), length.out = 25, time = "3 days")
time_elapsed(x)
time_elapsed(x, "days", rolling = FALSE)
# Grouped example
set.seed(99)
g <- sample.int(3, 25, TRUE)
time_elapsed(x, "days", g = g)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.