text: user248: I have a set of genomic ranges that are potentially overlapping. I want to count the amount of ranges at certain positions using R. 
  I'm Pretty sure there are good solutions, but I seem to be unable to find them. 
  Solutions like cut or findIntervals don't achieve what I want as they only count on one vector or accumulate by all values <= break.
  Also countMatches {GenomicRanges} doesn't seem to cover it.
  Probably one could use Bedtools, but I don't want to leave R.
  I could only come up with a hilariously slow solution
  # generate test data
  testdata <- data.frame(chrom = rep(seq(1,10),10),
                         starts = abs(rnorm(100, mean = 1, sd = 1)) * 1000,
                         ends = abs(rnorm(100, mean = 2, sd = 1)) * 2000)
  
  # make sure that all end coordinates are bigger than start
  # this is a requirement of the original data
  testdata <- testdata[testdata$ends - testdata$starts > 0,]
  
  # count overlapping ranges on certain positions
  count.data <- lapply(unique(testdata$chrom), function(chromosome){
      tmp.inner <- lapply(seq(1,10000, by = 120), function(i){
          sum(testdata$chrom == chromosome & testdata$starts <= i & testdata$ends >= i)
      })
      return(unlist(tmp.inner))
  })
  
  # generate a data.frame containing all data
  df.count.data <- ldply(count.data, rbind)
  
  # ideally the chromosome will be columns and not rows
  t(df.count.data)
  
  
  user29: GenomicRanges::countOverlaps seems to be what you’re after:
  position_range = GRanges(position$chrom, IRanges(position, position, width = 1))
  ranges_at_position = countOverlaps(position_ranges, granges)
  
  
original_text: 248: <p>I have a set of genomic ranges that are potentially overlapping. I want to count the amount of ranges at certain positions using R. </p>
  
  <p>I'm Pretty sure there are good solutions, but I seem to be unable to find them. </p>
  
  <p>Solutions like <a href="https://stackoverflow.com/questions/26140151/binning-two-vectors-of-different-ranges-using-r?rq=1">cut</a> or <a href="https://stackoverflow.com/questions/11963508/generate-bins-from-a-data-frame">findIntervals</a> don't achieve what I want as they only count on one vector or accumulate by <code>all values &lt;= break</code>.</p>
  
  <p>Also <code>countMatches {GenomicRanges}</code> doesn't seem to cover it.</p>
  
  <p>Probably one could use Bedtools, but I don't want to leave R.</p>
  
  <p>I could only come up with a hilariously slow solution</p>
  
  <pre><code># generate test data
  testdata &lt;- data.frame(chrom = rep(seq(1,10),10),
                         starts = abs(rnorm(100, mean = 1, sd = 1)) * 1000,
                         ends = abs(rnorm(100, mean = 2, sd = 1)) * 2000)
  
  # make sure that all end coordinates are bigger than start
  # this is a requirement of the original data
  testdata &lt;- testdata[testdata$ends - testdata$starts &gt; 0,]
  
  # count overlapping ranges on certain positions
  count.data &lt;- lapply(unique(testdata$chrom), function(chromosome){
      tmp.inner &lt;- lapply(seq(1,10000, by = 120), function(i){
          sum(testdata$chrom == chromosome &amp; testdata$starts &lt;= i &amp; testdata$ends &gt;= i)
      })
      return(unlist(tmp.inner))
  })
  
  # generate a data.frame containing all data
  df.count.data &lt;- ldply(count.data, rbind)
  
  # ideally the chromosome will be columns and not rows
  t(df.count.data)
  </code></pre>
   29: <p><a href="https://www.rdocumentation.org/packages/GenomicRanges/versions/1.24.1/topics/findOverlaps-methods" rel="noreferrer"><code>GenomicRanges::countOverlaps</code></a> seems to be what you’re after:</p>
  
  <pre><code>position_range = GRanges(position$chrom, IRanges(position, position, width = 1))
  ranges_at_position = countOverlaps(position_ranges, granges)
  </code></pre>
  