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
<chr> <int> <int>
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
<chr> <int> <int> <int>
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