# is_linear_sequence: Check whether a variable represents a linear sequence In validate: Data Validation Infrastructure

## Description

A variable X = (x_1, x_2,…, x_n) (n≥q 0) represents a linear sequence when x_{j+1} - x_j is constant for all j≥q 1. That is, elements in the series are equidistant and without gaps.

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75``` ```is_linear_sequence(x, by = NULL, ...) ## S3 method for class 'numeric' is_linear_sequence( x, by = NULL, begin = NULL, end = NULL, sort = TRUE, tol = 1e-08, ... ) ## S3 method for class 'Date' is_linear_sequence(x, by = NULL, begin = NULL, end = NULL, sort = TRUE, ...) ## S3 method for class 'POSIXct' is_linear_sequence( x, by = NULL, begin = NULL, end = NULL, sort = TRUE, tol = 1e-06, ... ) ## S3 method for class 'character' is_linear_sequence( x, by = NULL, begin = NULL, end = NULL, sort = TRUE, format = "auto", ... ) in_linear_sequence(x, ...) ## S3 method for class 'character' in_linear_sequence( x, by = NULL, begin = NULL, end = NULL, sort = TRUE, format = "auto", ... ) ## S3 method for class 'numeric' in_linear_sequence( x, by = NULL, begin = NULL, end = NULL, sort = TRUE, tol = 1e-08, ... ) ## S3 method for class 'Date' in_linear_sequence(x, by = NULL, begin = NULL, end = NULL, sort = TRUE, ...) ## S3 method for class 'POSIXct' in_linear_sequence( x, by = NULL, begin = NULL, end = NULL, sort = TRUE, tol = 1e-06, ... ) ```

## Arguments

 `x` An R vector. `by` bare (unquoted) variable name or a list of unquoted variable names, used to split `x` into groups. The check is executed for each group. `...` Arguments passed to other methods. `begin` Optionally, a value that should equal `min(x)` `end` Optionally, a value that should equal `max(x)` `sort` `[logical]`. When set to `TRUE`, `x` is sorted within each group before testing. `tol` numerical tolerance for gaps. `format` `[character]`. How to interpret `x` as a time period. Either `"auto"` for automatic detection or a specification passed to `strptime`. Automatically detected periods are of the form year: `"2020"`, yearMmonth: `"2020M01"`, yearQquarter: `"2020Q3"`, or year-Qquarter: `"2020-Q3"`.

## Details

Presence of a missing value (`NA`) in `x` will result in `NA`, except when `length(x) <= 2` and `start` and `end` are `NULL`. Any sequence of length ≤q 2 is a linear sequence.

## Value

For `is_linear_sequence`: a single `TRUE` or `FALSE`, equal to `all(in_linear_sequence)`.

For `in_linear_sequence`: a `logical` vector with the same length as `x`.

Other cross-record-helpers: `contains_exactly()`, `do_by()`, `exists_any()`, `hb()`, `hierarchy()`, `is_complete()`, `is_unique()`
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32``` ```is_linear_sequence(1:5) # TRUE is_linear_sequence(c(1,3,5,4,2)) # FALSE is_linear_sequence(c(1,3,5,4,2), sort=TRUE) # TRUE is_linear_sequence(NA_integer_) # TRUE is_linear_sequence(NA_integer_, begin=4) # FALSE is_linear_sequence(c(1, NA, 3)) # FALSE d <- data.frame( number = c(pi, exp(1), 7) , date = as.Date(c("2015-12-17","2015-12-19","2015-12-21")) , time = as.POSIXct(c("2015-12-17","2015-12-19","2015-12-20")) ) rules <- validator( is_linear_sequence(number) # fails , is_linear_sequence(date) # passes , is_linear_sequence(time) # fails ) summary(confront(d,rules)) ## check groupwise data dat <- data.frame( time = c(2012, 2013, 2012, 2013, 2015) , type = c("hi", "hi", "ha", "ha", "ha") ) rule <- validator(in_linear_sequence(time, by=type)) values(confront(dat, rule)) ## 2xT, 3xF rule <- validator(in_linear_sequence(time, type)) values( confront(dat, rule) ) ```