<p>There are two functions in the tidyverse suite (specifically <code>dplyr</code>) called <code>lead</code> and <code>lag</code> that allow calculations like this to be done:</p>
<pre class="lang-r prettyprint-override"><code>library(tidyverse)
G3_dat %&gt;%
   group_by(chrom) %&gt;%
   arrange(start) %&gt;%
   mutate(dis = start - lag(end))
</code></pre>
<p>I'm not sure if I've got the lead/lag function correct there (I've updated it based on a suggestion from @ram-rs), but it doesn't take too long to play round with all the combinations to get the right one.</p>
<p>FWIW, <code>%&gt;%</code> (or <code>|&gt;</code> now in native R) is a &quot;pipe&quot; operator that means &quot;take the result of the last function and feed it into the first argument of the next function&quot;. It is used extensively in <code>tidyverse</code> scripts. More details <a href="https://stackoverflow.com/questions/67633022/what-are-the-differences-between-rs-native-pipe-and-the-magrittr-pipe">here</a>.</p>

CC BY-SA 4.0
2024-02-10T09:33:30.397Z
2024-03-22T08:53:44.920Z
2024-03-22T08:53:44.920Z
gringer
<p>I'm wondering how can I calculate the distance between two regions from the bed (<code>chr</code>, <code>start</code>, <code>end</code>) alike dataframe in R. Precisely, I want to subtract <code>end</code> of r from <code>start</code> of r+1 (r=row). Also, the calculation will repeat similarly for each chromosome.</p>
<pre class="lang-r prettyprint-override"><code>head(G3_dat[1:3])

# A tibble: 6 × 3
  chrom   start     end
  &lt;chr&gt;   &lt;int&gt;   &lt;int&gt;
1 chr1  1330426 1330628
2 chr1  2017424 2017750
3 chr1  2017424 2017750
4 chr1  2018462 2018871
5 chr1  3899411 3899468
6 chr1  4653431 4653724
</code></pre>
<p>The expected outcome would be,</p>
<pre class="lang-r prettyprint-override"><code>head(G3_dat[1:4])

    # A tibble: 6 × 4
      chrom   start     end     dis
      &lt;chr&gt;   &lt;int&gt;   &lt;int&gt;     &lt;int&gt;
    1 chr1  1330426 1330628
    2 chr1  2017424 2017750     686796
    3 chr1  2017424 2017750       -326
    4 chr1  2018462 2018871        712
    5 chr1  3899411 3899468    1880540
    6 chr1  4653431 4653724     753963
</code></pre>

<r><genomics><bed><dataframe>
How to calculate distance between two regions from bed like dataframe in R?
# How to calculate distance between two regions from bed like dataframe in R?

(Asked by: username_0 on 2024-02-10)

I'm wondering how can I calculate the distance between two regions from the bed (chr, start, end) alike dataframe in R. Precisely, I want to subtract end of r from start of r+1 (r=row). Also, the calculation will repeat similarly for each chromosome.
head(G3_dat[1:3])

# A tibble: 6 × 3
  chrom   start     end
        
1 chr1  1330426 1330628
2 chr1  2017424 2017750
3 chr1  2017424 2017750
4 chr1  2018462 2018871
5 chr1  3899411 3899468
6 chr1  4653431 4653724

The expected outcome would be,
head(G3_dat[1:4])

    # A tibble: 6 × 4
      chrom   start     end     dis
                 
    1 chr1  1330426 1330628
    2 chr1  2017424 2017750     686796
    3 chr1  2017424 2017750       -326
    4 chr1  2018462 2018871        712
    5 chr1  3899411 3899468    1880540
    6 chr1  4653431 4653724     753963

**Tags:** r, genomics, bed, dataframe

---

## 1 answers

### Answer by username_1 on 2024-02-10. Score: 3

There are two functions in the tidyverse suite (specifically dplyr) called lead and lag that allow calculations like this to be done:
library(tidyverse)
G3_dat %>%
   group_by(chrom) %>%
   arrange(start) %>%
   mutate(dis = start - lag(end))

I'm not sure if I've got the lead/lag function correct there (I've updated it based on a suggestion from @ram-rs), but it doesn't take too long to play round with all the combinations to get the right one.
FWIW, %>% (or |> now in native R) is a "pipe" operator that means "take the result of the last function and feed it into the first argument of the next function". It is used extensively in tidyverse scripts. More details here.


---

