Answers.Body: <p>There is like a thousand different ways how to achieve this. You could use a specialised software for this (like bedtools) or calculate it simply in R.</p>
  
  <p>R solution: You can make a function that calculates the number of SNPs in a range and than you can apply it on all the ranges in the table with genomic ranges.</p>
  
  <pre><code>snp_table &lt;- read.table('WTSI-OESO_121_1pre.copynumber.txt')
  genomic_ranges &lt;- read.table('WTSI-OESO_121_1pre.copynumber.caveman.txt.csv', sep = ',', col.names = c('Chromosome', 'Start', 'End'), stringsAsFactors = F)
  
  how_many_in_range &lt;- function(coords){
      coords = as.vector(coords)
      sum(snp_table<span class="math-container">$Chromosome == coords[1] &amp; snp_table$</span>Position &gt; as.numeric(coords[2]) &amp; snp_table$Position &lt; as.numeric(coords[3]))
  }
  
  genomic_ranges$number_of_snps &lt;- apply(genomic_ranges, 1, how_many_in_range)
  </code></pre>
  
  <p>Now the table <code>genomic_ranges</code> has a variable that sais how many SNPs are in the range.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-26T21:03:17.740Z
Answers.Text: You know, the problem others have with your question is not about your coding skills, but more about the crossposting that really annoys the heck out of everybody, also your questions do not clearly show much research effort. It might be that you spent days trying to figure it out, but from your question, we can not tell, because you did not write what you have tried. Finally, I don't think self-hatred is useful to improve your coding skills, reading manuals and trying it out is way more efficient.
Answers.UserDisplayName: Kamil S Jaron
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-26T20:16:53.257Z
Answers.LastActivityDate: 2019-04-26T20:16:53.257Z
Answers.UserDisplayName: Kamil S Jaron
Body: <p>I have called copy number and I have 2 files (I have shared the link of my files ); One contains some ranges </p>
  
  <pre><code>&gt; head(cndata[,c(2,3,4)])
       Chromosome   Start      End
  4470          1   51479   817980
  4471          1  818499  1136753
  4472          1 1138735  2558308
  4473          1 2558740  5724264
  4474          1 5724940  5749083
  4475          1 5749226 12529544
  &gt; 
  </code></pre>
  
  <p><a href="https://www.dropbox.com/s/wei3i787c9nrz8q/WTSI-OESO_121_1pre.copynumber.caveman.txt.csv?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/wei3i787c9nrz8q/WTSI-OESO_121_1pre.copynumber.caveman.txt.csv?dl=0</a></p>
  
  <p>I have another file like below </p>
  
  <pre><code>              Chromosome         Position
  rs62635286       1                 51479   
  rs75454623       1                 114930
  rs806731         1                 30923
  </code></pre>
  
  <p><a href="https://www.dropbox.com/s/tiqjhkrpfr9d9gu/WTSI-OESO_121_1pre.copynumber.txt?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/tiqjhkrpfr9d9gu/WTSI-OESO_121_1pre.copynumber.txt?dl=0</a></p>
  
  <p>In first  column I have some SNPs, how I can count the number of SNP in each range? For example based on the second table how many SNP are in range of <code>51479</code> to  <code>817980</code>?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-26T12:26:29.463Z
Comments.Text: Repost of https://www.biostars.org/p/376315/
Comments.UserDisplayName: Wouter De Coster
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-26T12:52:43.053Z
Comments.Text: We need to ["merge with overlap/range"](https://stackoverflow.com/q/24480031/680068) then ["group by count"](https://stackoverflow.com/questions/9809166/count-number-of-rows-within-each-group), searching these terms should help you solve the problem.
Comments.UserDisplayName: zx8754
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-26T13:29:32.490Z
Comments.Text: Damian and Wouter have given you perfect solutions on BioStars. There is nothing more to add.
Comments.UserDisplayName: Emily_Ensembl
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-26T13:50:40.140Z
Comments.Text: @FereshTeh I think people are getting a bit upset if you crosspost in different sites and it's not like you are a new user who just does not know. You are here for a while and you know how this site works. You must have noticed that crossposting is not very popular and if you do, at least you can explicitly mention it in the question.
Comments.UserDisplayName: Kamil S Jaron
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-26T14:53:56.137Z
Comments.Text: Even better, if you get answers in one of the two places you ask, at least take the time to post them in the other. So if you get an answer here, also post it on your question at biostars and if you get an answer there, also post it here. That way, both communities benefit.
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-29T15:00:11.543Z
Comments.Text: @FereshTeh I found [a good article for you](https://stackoverflow.com/help/how-to-ask). Try always explain the problem (you do that moreless already), what have you tried to resolve it (this one you need to work on) and how to reproduce the problem you find (this one you do, but it would be nicer if you shared samples, not dropbox links).
Comments.UserDisplayName: Kamil S Jaron
Tags: <snp><bedtools><wgs><copy-number><text-mining>
Title: Extracting the number SNP in each range
ThreadText: # Extracting the number SNP in each range
  
  (Asked by: username_0 on 2019-04-26)
  
  I have called copy number and I have 2 files (I have shared the link of my files ); One contains some ranges
  
  ```
  > head(cndata[,c(2,3,4)])
       Chromosome   Start      End
  4470          1   51479   817980
  4471          1  818499  1136753
  4472          1 1138735  2558308
  4473          1 2558740  5724264
  4474          1 5724940  5749083
  4475          1 5749226 12529544
  > 
  
  ```
  
  <https://www.dropbox.com/s/wei3i787c9nrz8q/WTSI-OESO_121_1pre.copynumber.caveman.txt.csv?dl=0>
  
  I have another file like below
  
  ```
                Chromosome         Position
  rs62635286       1                 51479   
  rs75454623       1                 114930
  rs806731         1                 30923
  
  ```
  
  <https://www.dropbox.com/s/tiqjhkrpfr9d9gu/WTSI-OESO_121_1pre.copynumber.txt?dl=0>
  
  In first column I have some SNPs, how I can count the number of SNP in each range? For example based on the second table how many SNP are in range of `51479` to `817980`?
  
  **Tags:** snp, bedtools, wgs, copy-number, text-mining
  
  > **Comments:**
  > **username_1**: Repost of https://www.biostars.org/p/376315/  
  > **username_2**: We need to ["merge with overlap/range"](https://stackoverflow.com/q/24480031/680068) then ["group by count"](https://stackoverflow.com/questions/9809166/count-number-of-rows-within-each-group), searching these terms should help you solve the problem.  
  > **username_3**: Damian and username_1 have given you perfect solutions on BioStars. There is nothing more to add.  
  > **username_4**: @FereshTeh I think people are getting a bit upset if you crosspost in different sites and it's not like you are a new user who just does not know. You are here for a while and you know how this site works. You must have noticed that crossposting is not very popular and if you do, at least you can explicitly mention it in the question.  
  > **username_5**: Even better, if you get answers in one of the two places you ask, at least take the time to post them in the other. So if you get an answer here, also post it on your question at biostars and if you get an answer there, also post it here. That way, both communities benefit.  
  > **username_4**: @FereshTeh I found [a good article for you](https://stackoverflow.com/help/how-to-ask). Try always explain the problem (you do that moreless already), what have you tried to resolve it (this one you need to work on) and how to reproduce the problem you find (this one you do, but it would be nicer if you shared samples, not dropbox links).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_4 on 2019-04-26. Score: 4 | Accepted answer
  
  There is like a thousand different ways how to achieve this. You could use a specialised software for this (like bedtools) or calculate it simply in R.
  
  R solution: You can make a function that calculates the number of SNPs in a range and than you can apply it on all the ranges in the table with genomic ranges.
  
  ```
  snp_table <- read.table('WTSI-OESO_121_1pre.copynumber.txt')
  genomic_ranges <- read.table('WTSI-OESO_121_1pre.copynumber.caveman.txt.csv', sep = ',', col.names = c('Chromosome', 'Start', 'End'), stringsAsFactors = F)
  
  how_many_in_range <- function(coords){
      coords = as.vector(coords)
      sum(snp_table$Chromosome == coords[1] & snp_table$Position > as.numeric(coords[2]) & snp_table$Position < as.numeric(coords[3]))
  }
  
  genomic_ranges$number_of_snps <- apply(genomic_ranges, 1, how_many_in_range)
  
  ```
  
  Now the table `genomic_ranges` has a variable that sais how many SNPs are in the range.
  
  > **Comments:**
  > **username_4**: You know, the problem others have with your question is not about your coding skills, but more about the crossposting that really annoys the heck out of everybody, also your questions do not clearly show much research effort. It might be that you spent days trying to figure it out, but from your question, we can not tell, because you did not write what you have tried. Finally, I don't think self-hatred is useful to improve your coding skills, reading manuals and trying it out is way more efficient.  
  
  ---
  
  