R: Check variable range

in_range {validate} | R Documentation

## Check variable range

### Description

Test wether a variable falls within a range.

### Usage

    in_range(x, min, max, ...)

    ## Default S3 method:
    in_range(x, min, max, strict = FALSE, ...)

    ## S3 method for class 'character'
    in_range(x, min, max, strict = FALSE, format = "auto", ...)

### Arguments

`x` | A bare (unquoted) variable name.
`min` | lower bound
`max` | upper bound
`...` | arguments passed to other methods
`strict` | `[logical]` Toggle between including the range boundaries (default) or not including them (when strict=TRUE).
`format` | `[character]` of `NULL`. If `format=NULL` the character vector is interpreted as is. And the whether a character lies within a character range is determined by the collation order set by the current locale. See the details of "`<`". If `format` is not `NULL`, it specifies how to interpret the character vector as a time period. It can take the value `"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"`.

### Examples

    d <- data.frame(
       number = c(3,-2,6)
     , time   = as.Date(c("2018-02-01", "2018-03-01", "2018-04-01"))
     , period = c("2020Q1", "2021Q2", "2020Q3")
    )

    rules <- validator(
       in_range(number, min=-2, max=7, strict=TRUE)
     , in_range(time,   min=as.Date("2017-01-01"), max=as.Date("2018-12-31"))
     , in_range(period, min="2020Q1", max="2020Q4")
    )

    result <- confront(d, rules)
    values(result)

[Package validate version 1.1.1 Index]