time_id {timeplyr} | R Documentation |
Time ID
Description
Generate a time ID that signifies how many time steps away a time value is from the starting time point or more intuitively, this is the time passed since the first time point.
Usage
time_id(
x,
time_by = NULL,
g = NULL,
na_skip = TRUE,
time_type = getOption("timeplyr.time_type", "auto"),
shift = 1L
)
Arguments
x |
Time variable. |
time_by |
Time unit.
|
g |
Object used for grouping x.
This can for example be a vector or data frame.
|
na_skip |
Should |
time_type |
If "auto", |
shift |
Value used to shift the time IDs. Typically this is 1 to ensure the IDs start at 1 but can be 0 or even negative if for example your time values are going backwards in time. |
Details
This is heavily inspired by collapse::timeid
but differs in 3 ways:
The time steps need not be the greatest common divisor of successive differences
The starting time point may not necessarily be the earliest chronologically and thus
time_id
can generate negative IDs.-
g
can be supplied to calculate IDs by group.
time_id(c(3, 2, 1))
is not the same as collapse::timeid(c(3, 2, 1))
.
In general time_id(sort(x))
should be equal to collapse::timeid(sort(x))
.
The time difference GCD is always calculated using all the data and not
by-group.
Value
An integer vector the same length as x
.