mardi 14 janvier 2020

Difference between consecutive rows using an if statement but condition has length > 1

I have seen similar questions to mine but could not find the way to solve it in this specific case. I am trying to do the difference between values from two consecutive rows when the value in another column is negative. If not, I want to copy the value from another column.

    symbol  Strt    End len Overlap
121 TPTE2P4 26508213    26579690    1605    153795
46  CYCSP49 26549425    26549743    319 -30265
116 SLC25A15P1  26586642    26591601    342 36899
84  PARP4P1 26594851    26634652    2337    3250

So with the date from above, I am trying to do the difference between values from two consecutive rows (End of the current row -Start from the previous row) when the value in "Overlap" column is negative. If not, I want to copy the value from "len". I should expect these results:

    symbol  Strt    End len Overlap len_no_overlap
121 TPTE2P4 26508213    26579690    1605    153795  1605
46  CYCSP49 26549425    26549743    319 -30265 **41530**
116 SLC25A15P1  26586642    26591601    342 36899   342
84  PARP4P1 26594851    26634652    2337    3250    2337

So in this case, only in the second row the "Overlap" is negative, and the "len_no_overlap" value comes from 26549743-26508213.

I have written the code as follows but can not vectorise it to work

if (DPM_356_out_High_loss$Overlap < 0) {
   DPM_356_out_High_loss$len_no_overlap <- c(NA, tail(DPM_356_out_High_loss$End, -1)      head(DPM_356_out_High_loss$Strt, -1))
   } else {
       DPM_356_out_High_loss$len_no_overlap <- DPM_356_out_High_loss$len
   }

Any help with this would be much appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire