This comes as an application to this question:Sum object in a column between an interval defined by another column
What I would like to know is how to adjust the answer if I want to sum the values in B, for ((A[i+1]-A[i]==0)
or (A[i+1]-A[i]==1)
or (A[i]-A[i-1]==0)
or (A[i]-A[i-1]==1))
where i
is the row index, so basically sum B rows for A-s that have the same value +/- 1, but not sum the same row twice?
I tried building a loop function but I get stuck when using row indices with data frames. Example: If the following data frame is given
df
A B
[1,] 1 4
[2,] 1 3
[3,] 3 5
[4,] 3 7
[5,] 4 3
[6,] 5 2
What I want to obtain is the next data frame:
df
A B
[1,] 1 7
[2,] 3 15
[3,] 5 2
Moreover if a have a large data frame like this:
df
chr start stop m n s
chr1 71533361 71533362 23 1 -
chr1 71533361 71533362 24 26 -
chr1 71533361 71533362 25 1 -
and I want my result to look like this (I chose the row for which the value in column m is max):
df
chr1 71533361 71533362 24 28 -