time_seq_id {timeplyr} | R Documentation |
Generate a unique identifier for a regular time sequence with gaps
Description
A unique identifier is created every time a specified amount of time has passed, or in the case of regular sequences, when there is a gap in time.
Usage
time_seq_id(
x,
time_by = NULL,
threshold = 1,
g = NULL,
na_skip = TRUE,
rolling = TRUE,
switch_on_boundary = FALSE,
time_type = getOption("timeplyr.time_type", "auto")
)
Arguments
x |
Date, datetime or numeric vector. |
time_by |
Time unit.
|
threshold |
Threshold such that when the time elapsed
exceeds this, the sequence ID is incremented by 1.
For example, if |
g |
Object used for grouping x.
This can for example be a vector or data frame.
|
na_skip |
Should |
rolling |
When this is |
switch_on_boundary |
When an exact amount of time
(specified in |
time_type |
If "auto", |
Details
time_seq_id()
Assumes x
is regular and in
ascending or descending order.
To check this condition formally, use time_is_regular()
.
Value
An integer vector of length(x)
.
Examples
library(dplyr)
library(timeplyr)
library(lubridate)
# Weekly sequence, with 2 gaps in between
x <- time_seq(today(), length.out = 10, time_by = "week")
x <- x[-c(3, 7)]
# A new ID when more than a week has passed since the last time point
time_seq_id(x, time_by = "week")
# A new ID when >= 2 weeks has passed since the last time point
time_seq_id(x, time_by = "weeks", threshold = 2, switch_on_boundary = TRUE)
# A new ID when at least 4 cumulative weeks have passed
time_seq_id(x, time_by = "4 weeks",
switch_on_boundary = TRUE, rolling = FALSE)
# A new ID when more than 4 cumulative weeks have passed
time_seq_id(x, time_by = "4 weeks",
switch_on_boundary = FALSE, rolling = FALSE)