[R] findInterval and data resolution
Duncan Murdoch
murdoch.duncan at gmail.com
Mon Jul 12 23:38:06 CEST 2010
On 12/07/2010 5:25 PM, Bryan Hanson wrote:
> 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.
I'm not sure I understand what you want. If you know x1 will always be
an increasing vector, you could use something like a2[1]:a2[length(a2)]
to select the full range of indices that it covers. If x1 is not
necessarily in increasing order, you'll have to do min(a2):max(a2)
(which might be clearer in any case).
If you're more interested in the range of values in vec*, maybe
range(vec2[min(a2):max(a2)])
will give you want you want.
Duncan Murdoch
More information about the R-help
mailing list