[Rd] findInterval
Gabor Grothendieck
ggrothend|eck @end|ng |rom gm@||@com
Mon Sep 16 17:21:55 CEST 2024
Suppose we have `dat` shown below and we want to find the the `y` value
corresponding to the last value in `x` equal to the corresponding component
of `seek` and we wish to return an output the same length as `seek` using
`findInterval` to perform the search. This returns the correct result:
dat <- data.frame(x = c(2, 2, 3, 4, 4, 4),
y = c(37, 12, 19, 30, 6, 15),
seek = 1:6)
zero2na <- function(x) replace(x, x == 0, NA)
dat |>
transform(dat, result = y[ zero2na(findInterval(seek, x)) ] ) |>
_$result
## [1] NA 12 19 15 15 15
Since `findInterval` returns an index it is natural that the next step be
to use the index and it is also common that we want a result that is the
same length as the input.
The extra step here is to convert the 0 which `findInterval`
hard codes as missing to NA.
If, like `match`, the `findInterval` function had a `nomatch=` argument we
could have written this as follows which is shorter, more understandable
and avoids the need for zero2na:
# if nomatch= were implemented
seek |>
transform(result = y[ findInterval(x, nomatch = NA) ] ) |>
_$result
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-devel
mailing list