Answers.Body: <p>This command gave me the number of axons
  I hope this is correct</p>
  
  <p>Please correct me if I am wrong</p>
  
  <pre><code>how_many_in_range &lt;- function(coords){
      coords = as.vector(coords)
      sum(human_exome_coordinates<span class="math-container">$Choromosome == coords[1] &amp; human_exome_coordinates$</span>Position &gt; as.numeric(coords[2]) &amp; human_exome_coordinates$Position &lt; as.numeric(coords[3]))
  }
  
  genomic_ranges$number_of_exon &lt;- apply(segment_mean_table, 1, how_many_in_range)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-03T09:51:40.077Z
Answers.LastActivityDate: 2020-04-03T09:51:40.077Z
Answers.UserDisplayName: Zizogolu
Answers.Body: <p>Regardless of what your specific data is, the goal of your analysis is really to find overlaps between two data frames of genomic coordinations. My favorite package for doing this is <a href="https://bioconductor.org/packages/release/bioc/html/GenomicRanges.html" rel="nofollow noreferrer">GenomicRanges</a>. It is very fast and can take care of different strands (if your data contains strand information).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-03T08:48:05.543Z
Answers.Text: Sorry I am too stupid in calculation, how I can do that?
Answers.UserDisplayName: Zizogolu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-03T16:48:33.380Z
Answers.Text: Just a quick example, suppose I have two data frames. `df1.gr=GRanges(Rle(df1$chr), IRanges(df1$start, df1$end))`, `df2.gr=GRanges(Rle(df2$chr), IRanges(df2$start, df2$end))`. And then `ov = findOverlaps(df1.gr, df2.gr)`. You can also customize some arguments in the findOverlaps function. You can also find more on the GenomicRanges webpage.
Answers.UserDisplayName: Phoenix Mu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-03T01:41:23.490Z
Answers.LastActivityDate: 2020-04-03T01:41:23.490Z
Answers.UserDisplayName: Phoenix Mu
Body: <p>I have a big data frame like this for copy number (exome seq)</p>
  
  <pre><code>    &gt; head(df)
    Chromosome    Start      End
  1 1             64613  5707515
  2 1           5712940  5732322
  3 1           5732399 16383682
  4 1          16383742 16389288
  5 1          16390813 16830026
  6 1          16830201 17278112
  </code></pre>
  
  <p>I also have coordinate of human exams like this</p>
  
  <pre><code>Exone   Choromosome     Start_of_exone  End_of_exone 
  uc001aaa.3  chr1    11873   12227
  uc001aaa.3  chr1    12612   12721
  uc001aaa.3  chr1    13220   14409
  uc010nxr.1  chr1    11873   12227
  uc010nxr.1  chr1    12645   12697
  uc010nxr.1  chr1    13220   14409
  uc010nxq.1  chr1    11873   12227
  uc010nxq.1  chr1    12594   12721
  uc010nxq.1  chr1    13402   14409
  </code></pre>
  
  <p>Actually I need to interset my copy number coordinates and human exome coordinates to see there are how many exomes in these coordinates</p>
  
  <p>I have used this code but all returns 0</p>
  
  <pre><code>snp_table &lt;- expanse coordinate file
  genomic_ranges &lt;- df
  
  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>Can you help me?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-02T16:00:35.080Z
Comments.Text: I am wondering how you got that calculation from google about the X2 thing. Can you describe in detail what is in your data frame?
Comments.UserDisplayName: Phoenix Mu
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-02T17:41:57.560Z
Comments.Text: I'm sure that's not what @PhoenixMu is asking you - you're literally just telling us what the header already tells us. Some real information would give us context on the data contained in the data frame. Are they probes? exons? random regions? And "google says" is not a valid citation. What is the source of your information on probes to exons ratio?
Comments.UserDisplayName: Ram RS
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-02T19:01:05.050Z
Comments.Text: This is called by "merge by overlap" see for example this (my preferred) answer using [data.table::foverlap()](https://stackoverflow.com/a/27672188/680068)
Comments.UserDisplayName: zx8754
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-02T20:29:10.737Z
Comments.Text: Merge then aggregate.
Comments.UserDisplayName: zx8754
Tags: <r><bash><cnv><exome><genomicranges>
Title: Calculating the number of probes for a given genomic range
ThreadText: # Calculating the number of probes for a given genomic range
  
  (Asked by: username_0 on 2020-04-02)
  
  I have a big data frame like this for copy number (exome seq)
  
  ```
      > head(df)
    Chromosome    Start      End
  1 1             64613  5707515
  2 1           5712940  5732322
  3 1           5732399 16383682
  4 1          16383742 16389288
  5 1          16390813 16830026
  6 1          16830201 17278112
  
  ```
  
  I also have coordinate of human exams like this
  
  ```
  Exone   Choromosome     Start_of_exone  End_of_exone 
  uc001aaa.3  chr1    11873   12227
  uc001aaa.3  chr1    12612   12721
  uc001aaa.3  chr1    13220   14409
  uc010nxr.1  chr1    11873   12227
  uc010nxr.1  chr1    12645   12697
  uc010nxr.1  chr1    13220   14409
  uc010nxq.1  chr1    11873   12227
  uc010nxq.1  chr1    12594   12721
  uc010nxq.1  chr1    13402   14409
  
  ```
  
  Actually I need to interset my copy number coordinates and human exome coordinates to see there are how many exomes in these coordinates
  
  I have used this code but all returns 0
  
  ```
  snp_table <- expanse coordinate file
  genomic_ranges <- df
  
  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)
  
  ```
  
  Can you help me?
  
  **Tags:** r, bash, cnv, exome, genomicranges
  
  > **Comments:**
  > **username_1**: I am wondering how you got that calculation from google about the X2 thing. Can you describe in detail what is in your data frame?  
  > **username_2**: I'm sure that's not what @username_1 is asking you - you're literally just telling us what the header already tells us. Some real information would give us context on the data contained in the data frame. Are they probes? exons? random regions? And "google says" is not a valid citation. What is the source of your information on probes to exons ratio?  
  > **username_3**: This is called by "merge by overlap" see for example this (my preferred) answer using [data.table::foverlap()](https://stackoverflow.com/a/27672188/680068)  
  > **username_3**: Merge then aggregate.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2020-04-03. Score: 0 | Accepted answer
  
  This command gave me the number of axons
  I hope this is correct
  
  Please correct me if I am wrong
  
  ```
  how_many_in_range <- function(coords){
      coords = as.vector(coords)
      sum(human_exome_coordinates$Choromosome == coords[1] & human_exome_coordinates$Position > as.numeric(coords[2]) & human_exome_coordinates$Position < as.numeric(coords[3]))
  }
  
  genomic_ranges$number_of_exon <- apply(segment_mean_table, 1, how_many_in_range)
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2020-04-03. Score: 1
  
  Regardless of what your specific data is, the goal of your analysis is really to find overlaps between two data frames of genomic coordinations. My favorite package for doing this is [GenomicRanges](https://bioconductor.org/packages/release/bioc/html/GenomicRanges.html). It is very fast and can take care of different strands (if your data contains strand information).
  
  > **Comments:**
  > **username_0**: Sorry I am too stupid in calculation, how I can do that?  
  > **username_1**: Just a quick example, suppose I have two data frames. `df1.gr=GRanges(Rle(df1$chr), IRanges(df1$start, df1$end))`, `df2.gr=GRanges(Rle(df2$chr), IRanges(df2$start, df2$end))`. And then `ov = findOverlaps(df1.gr, df2.gr)`. You can also customize some arguments in the findOverlaps function. You can also find more on the GenomicRanges webpage.  
  
  ---
  
  