typestar

Clamp values in R

Bounds a vector between a floor and a ceiling using pmin and pmax.

clamp <- function(x, lo, hi) {
  pmin(pmax(x, lo), hi)
}

lerp <- function(a, b, t) {
  a + (b - a) * clamp(t, 0, 1)
}

Keywords and builtins used here

The run, in numbers

Lines
7
Characters to type
114
Tokens
51
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 36 seconds.

Type this snippet

Step 4 of 5 in Flow & functions, step 8 of 20 in Language basics.

← Previous Next →