fence: Limit values to a minimum and maximum

View source: R/fence.R

fenceR Documentation

Limit values to a minimum and maximum

Description

Clip / clamp / or fence, i.e. limit, values to a lower and upper bound. Similar to raster::clamp(). This is a wrapper around base::pmin() and base::pmax(); see those for more details.

Usage

fence(x, lower = -Inf, upper = Inf, na_rm = FALSE)

Arguments

x

Vector of numeric values.

lower

Numeric. Lowest value.

upper

Numeric. Highest value.

na_rm

A logical indicating whether missing values should be removed.

Value

A numeric vector

References

https://stackoverflow.com/questions/13868963/clip-values-between-a-minimum-and-maximum-allowed-value-in-r

Examples

#### Example 1 --------------------------------

vec <- sample(x = c(1:10),
       size = 10,
       replace = TRUE)

vec

fence(x = vec,
      lower = 4,
      upper = 6)

#### Example 2 --------------------------------

library(dplyr)
library(tibble)

df <- tibble::tibble(
  vec = vec
)

df %>%
  mutate(vec = fence(x = vec,
                     lower = 4,
                     upper = 6))

emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.