Hi all,

In R, I have a data frame with physical position of some genes in bp on the left and intervals (again in bp) on the right. I want to check for each gene, whether it falls in the the interval of the same chromosome.

```
GeneID       Chr       Position                Interval
x            1         18697251                1:3099740-8804450
y            1         19546617                1:3422930-8804450
z            1         3332236                 2:2751757-4502486
a            2         3993537                 2:6187995-8804450
b            2         9983384                 2:4864334-18271005
c            2         11479025                2:8222941-18271005
```

For example: Is gene x on chr 1 comprised in one of the 2 intervals displayed for chr 1? I expect TRUE or FALSE.

I managed to do this giving the gene positions one by one, but the list is very big, I'd like this to be automated.

Any help much appreciated!
I have a solution with a loop, although it is not very elegant (code not tested, you may need to debug it). I assume that all your data is as factors (this is why I use `as.numeric(as.character())` for instance).

    n=length(myData$GeneID)
    answer=rep(NA,n)   # this will be an array of TRUE/FALSE
    for (i in 1:n){
      temp = strsplit(as.character(myData$Interval[i]),":")[[1]]  # that gives you c("1","309974-880445")
      answer[i] = temp[1]==as.character(myData$Chr[i])  #first test chromosome
      if (answer[i]){    # no need to go further if the chromosome is wrong
        temp = strsplit(temp[2],"-")[[1]]   # that gives you c("309974","880445")
        pos = as.numeric(as.character(myData$Position[i])) # in case your data is as factor
       # Now lets test if position is between intervals.
       # I assume here that the interval can be in the "wrong" order and that its ok for you.
        answer[i] = (pos < temp[1] & pos > temp2) | (pos > temp[1] & pos < temp2)
      }
    }
    
    round(sum(answer)/length(answer)*100,2) # give you percentage of rightly positioned genes.
    myData$GeneID[!answer]  # give you the names of the wrongly positionned genes.