findInterval
Find Interval Numbers or Indices

Description

Finds the intervals between the elements of vec that contain the elements of x.

Usage

findInterval(x, vec, rightmost.closed = FALSE, all.inside = FALSE, left.open = FALSE)

Arguments

x a numeric vector. NAs are accepted and are mapped to NAs in the output.
vec a numeric vector in non-decreasing order. NAs are not accepted; -Inf and +Inf are accepted.

rightmost.closed a logical value. If FALSE, the default, all intervals, including the outermost ones, are closed on one end and open on the other, as determined by the left.open argument. If TRUE, the outer ends of both of the outermost intervals are closed.
all.inside a logical value. If TRUE, and given that N is length of vec, all indices must be inside of 1 to (N-1). The default is FALSE.
left.open a logical value.
  • If FALSE, the default, intervals are generally closed on the left (lower endpoint) and open on the right (upper endpoint). Therefore, x[j] is in interval i if vec[i] <= x[j] < vec[i+1].
  • If TRUE, intervals are open on the left and closed on the right. rightmost.closed affects the treatment of the outermost open endpoint.

Details

The ith interval is the interval between vec[i] and vec[i+1]. There are length(vec)-1 'inside' intervals, in addition to possible 'outside' intervals from -Inf to x[1] and from vec[length(vec)] to +Inf. The rightmost.closed and left.open arguments determine which interval the points of vec are in.
Value
returns an integer vector with the same length as x containing the indices of the intervals that contain the elements of x. Interval 0 contains the numbers below vec[1] and interval length(x) contains those above x[length(x)].
See Also
sort
Examples
x <- c(2, 3, 9, 4, 5.5, 1, 6, NA, 5)
vec <- c(2.5, 3.5, 5.5)
 findInterval(x, vec)
# 0  1  3  2  3  0  3 NA  2
 findInterval(x[!is.na(x)], vec)
# 0  1  3 2  3  0  3  2
findInterval(x, vec, rightmost.closed = TRUE)
# 0  1  3  2  2  0  3 NA  2
findInterval(x, vec, all.inside = TRUE)
# 1  1  2  2  2  1  2 NA  2
Package base version 6.0.0-69
Package Index