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
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
How about this: these = which(vec2 < x1[1] | vec2 > x1[2]) vec2[these] # Or using logical indexation directly: vec2[vec2 < x1[1] | vec2 > x1[2]]>>>From: Bryan Hanson <hanson@depauw.edu> To:R Help <r-help@stat.math.ethz.ch> Date: 13/Jul/2010 9:28a Subject: [R] findInterval and data resolution 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 ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R ( http://www.r/ )-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]