[R] findInterval and data resolution

Bryan Hanson hanson at depauw.edu
Mon Jul 12 23:25:03 CEST 2010

Hello Wise Ones...

I need a clever way around a problem with findInterval.  Consider:

vec1 <- 1:10
vec2 <- seq(1, 10, by = 0.1)

x1 <- c(2:3)

a1 <- findInterval(x1, vec1); a1 # example 1
a2 <- findInterval(x1, vec2); a2  # example 2

In the problem I'm working on, vec* may be either integer or numeric, like
vec1 and vec2.  I need to remove one or more sections of this vector; for
instance if I ask to remove values 2:3 I want to remove all values between 2
and 3 regardless of the resolution of the data (in my thinking, vec2 is more
dense or has better resolution than vec1).  So example 1 above works fine
because the values 2 and 3 are the end points of a range that includes no
values in-between (a1).  But, in example 2 the answer is, correctly, also
the end points, but now there are values in between these end points.  Hence
a2 doesn't include the indices of the values in-between the end points.

I have looked at cut, but it doesn't quite behave the way I want since if I
set x1 <- c(2:4) I get more intervals than I really want and cleaning it up
will be laborious.  I think I can construct the full set of indices I want
with a2[1]:a2[2] but is there a more clever way to do this?  I'm thinking
there might be a function out there that I am not aware of.

TIA, Bryan
Bryan Hanson
Acting Chair
Professor of Chemistry & Biochemistry
DePauw University, Greencastle IN USA

More information about the R-help mailing list