Learn R Programming

Kmisc (version 0.5.0)

in_interval: Determine if Value Lies within Interval

Description

This function determines whether elements of a numeric vector x lie within boundaries [lo, hi). Marginally slower than the R equivalent code x >= lo & x < hi for small vectors; much faster for very large vectors.

Usage

in_interval(x, lo, hi, include.lower = TRUE, include.upper = FALSE)

Arguments

x
numeric. vector of numbers.
lo
numeric, length 1. lower boundary.
hi
numeric, length 1. upper boundary.
include.lower
boolean. include the lower endpoint?
include.upper
boolean. include the upper endpoint?

Examples

Run this code
x <- runif(100); lo <- 0.5; hi <- 1
f <- function(x, lo, hi) {
  return( x >= lo & x < hi )
}
stopifnot( all( in_interval( x, lo, hi ) == f(x, lo, hi) ) )

Run the code above in your browser using DataLab