[R] range segment exclusion using range endpoints
jim holtman
jholtman at gmail.com
Sat May 12 20:50:02 CEST 2012
Here is an example of how you might do it. It uses a technique of
counting how many items are in a queue based on their arrival times;
it can be used to also find areas of overlap.
Note that it would be best to use a list for the 's' end points
================================
> # note the next statement removes names of the format 's[0-9]+_rng'
> # it would be best to create a list with the 's' endpoints, but this is
> # what the OP specified
>
> rm(list = grep('s[0-9]+_rng', ls(), value = TRUE)) # Danger Will Robinson!!
>
> # ex 1
> x_rng = c(-100,100)
>
> s1_rng = c(-25.5,30)
> s2_rng = c(0.77,10)
> s3_rng = c(25,35)
> s4_rng = c(70,80.3)
> s5_rng = c(90,95)
>
> # ex 2
> # x_rng = c(-50.5,100)
>
> # s1_rng = c(-75.3,30)
>
> # ex 3
> # x_rng = c(-75.3,30)
>
> # s1_rng = c(-50.5,100)
>
> # ex 4
> # x_rng = c(-100,100)
>
> # s1_rng = c(-105,105)
>
> # find all the names -- USE A LIST NEXT TIME
> sNames <- grep("s[0-9]+_rng", ls(), value = TRUE)
>
> # initial matrix with the 'x' endpoints
> queue <- rbind(c(x_rng[1], 1), c(x_rng[2], 1))
>
> # add the 's' end points to the list
> # this will be used to determine how many things are in a queue (or areas that
> # overlap)
> for (i in sNames){
+ queue <- rbind(queue
+ , c(get(i)[1], 1) # enter queue
+ , c(get(i)[2], -1) # exit queue
+ )
+ }
> queue <- queue[order(queue[, 1]), ] # sort
> queue <- cbind(queue, cumsum(queue[, 2])) # of people in the queue
> print(queue)
[,1] [,2] [,3]
[1,] -100.00 1 1
[2,] -25.50 1 2
[3,] 0.77 1 3
[4,] 10.00 -1 2
[5,] 25.00 1 3
[6,] 30.00 -1 2
[7,] 35.00 -1 1
[8,] 70.00 1 2
[9,] 80.30 -1 1
[10,] 90.00 1 2
[11,] 95.00 -1 1
[12,] 100.00 1 2
>
> # print out values where the last column is 1
> for (i in which(queue[, 3] == 1)){
+ cat("start:", queue[i, 1L], ' end:', queue[i + 1L, 1L], "\n")
+ }
start: -100 end: -25.5
start: 35 end: 70
start: 80.3 end: 90
start: 95 end: 100
>
>
=========================================
On Sat, May 12, 2012 at 1:54 PM, Ben quant <ccquant at gmail.com> wrote:
> Hello,
>
> I'm posting this again (with some small edits). I didn't get any replies
> last time...hoping for some this time. :)
>
> Currently I'm only coming up with brute force solutions to this issue
> (loops). I'm wondering if anyone has a better way to do this. Thank you for
> your help in advance!
>
> The problem: I have endpoints of one x range (x_rng) and an unknown number
> of s ranges (s[#]_rng) also defined by the range endpoints. I'd like to
> remove the x ranges that overlap with the s ranges. The examples below
> demonstrate what I mean.
>
> What is the best way to do this?
>
> Ex 1.
> For:
> x_rng = c(-100,100)
>
> s1_rng = c(-25.5,30)
> s2_rng = c(0.77,10)
> s3_rng = c(25,35)
> s4_rng = c(70,80.3)
> s5_rng = c(90,95)
>
> I would get:
> -100,-25.5
> 35,70
> 80.3,90
> 95,100
>
> Ex 2.
> For:
> x_rng = c(-50.5,100)
>
> s1_rng = c(-75.3,30)
>
> I would get:
> 30,100
>
> Ex 3.
> For:
> x_rng = c(-75.3,30)
>
> s1_rng = c(-50.5,100)
>
> I would get:
> -75.3,-50.5
>
> Ex 4.
> For:
> x_rng = c(-100,100)
>
> s1_rng = c(-105,105)
>
> I would get something like:
> NA,NA
> or...
> NA
>
> Ex 5.
> For:
> x_rng = c(-100,100)
>
> s1_rng = c(-100,100)
>
> I would get something like:
> -100,-100
> 100,100
> or just...
> -100
> 100
>
> PS - You may have noticed that in all of the examples I am including the s
> range endpoints in the desired results, which I can deal with later in my
> program so its not a problem... I think leaving in the s range endpoints
> simplifies the problem.
>
> Thanks!
> Ben
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
More information about the R-help
mailing list