time_is_regular {timeplyr} | R Documentation |
Is time a regular sequence? (Experimental)
Description
This function is a fast way to check if a time vector
is a regular sequence, possibly for many groups.
Regular in this context means that the lagged time differences are a
whole multiple of the specified time unit.
This means x
can be a regular sequence with or without gaps in time.
Usage
time_is_regular(
x,
time_by = NULL,
g = NULL,
use.g.names = TRUE,
na.rm = TRUE,
time_type = getOption("timeplyr.time_type", "auto"),
allow_gaps = TRUE,
allow_dups = TRUE
)
Arguments
x |
A vector. Can be a
|
time_by |
Time unit.
|
g |
Grouping object passed directly to |
use.g.names |
Should the result include group names?
Default is |
na.rm |
Should |
time_type |
If "auto", |
allow_gaps |
Should gaps be allowed? Default is |
allow_dups |
Should duplicates be allowed? Default is |
Value
A logical vector the same length as the number of supplied groups.
Examples
library(timeplyr)
library(lubridate)
library(dplyr)
x <- 1:5
y <- c(1, 1, 2, 3, 5)
time_is_regular(x)
time_is_regular(y)
increment <- 1
# No duplicates allowed
time_is_regular(x, increment, allow_dups = FALSE)
time_is_regular(y, increment, allow_dups = FALSE)
# No gaps allowed
time_is_regular(x, increment, allow_gaps = FALSE)
time_is_regular(y, increment, allow_gaps = FALSE)
# Grouped
eu_stock <- ts_as_tibble(EuStockMarkets)
eu_stock <- eu_stock %>%
mutate(date = as_date(
date_decimal(time)
))
time_is_regular(eu_stock$date, g = eu_stock$group,
time_by = 1)
# This makes sense as no trading occurs on weekends and holidays
time_is_regular(eu_stock$date, g = eu_stock$group,
time_by = 1,
allow_gaps = FALSE)