[BioC] Is a number within a set of ranges?

Oleg Sklyar osklyar at ebi.ac.uk
Mon Oct 29 21:16:51 CET 2007


This is a trivial one-liner:

r = data.frame(Start=c(1,5,13), End=c(3,9,15))
g = data.frame(Gene=c(1,2,3,4), Position=c(14,4,10,6))

index = apply(g, 1, function(x) any(x[2]>=r$Start & x[2]<=r$End))

> index
[1]  TRUE FALSE FALSE  TRUE
> g[index,]
  Gene Position
1    1       14
4    4        6


Best,
Oleg
-  
Dr Oleg Sklyar * EMBL-EBI, Cambridge CB10 1SD, UK * +441223494466


On Mon, 2007-10-29 at 16:29 +0000, Daniel Brewer wrote:
> I have a table with a start and stop column which defines a set of
> ranges.  I have another table with a list of genes with associated
> position.  What I would like to do is subset the gene table so it only
> contains genes whose position is within any of the ranges.  What is the
> best way to do this?  The only way I can think of is to construct a long
> list of conditions linked by ORs but I am sure there must be a better way.
> 
> Simple example:
> 
> Start	Stop
> 1	3
> 5	9
> 13	15
> 
> Gene	Position
> 1	14
> 2	4
> 3	10
> 4	6
> 
> I would like to get out:
> Gene	Position
> 1	14
> 4	6
> 
> Any ideas?
> 
> Thanks
> 
> Dan
>



More information about the Bioconductor mailing list