Skip to contents

Restricts x to the interval [min_val, max_val]. Values outside the range are replaced by the nearest bound.

Usage

clamp(x, min_val, max_val, warn = TRUE, param_name = "value")

Arguments

x

Numeric value or vector

min_val

Lower bound

max_val

Upper bound

warn

Logical; issue a warning when clamping occurs, default TRUE

param_name

Name used in warning messages

Value

x with values outside [min_val, max_val] replaced by the bounds

Examples

if (FALSE) { # \dontrun{
clamp(1.5, 0, 1)             # 1.0 with warning
clamp(-0.5, 0, 1)            # 0.0 with warning
clamp(0.3, 0, 1)             # 0.3 (no warning)
clamp(c(-1, 0.5, 2), 0, 1)  # c(0, 0.5, 1) with warning
} # }