I have a data frame that have a column for Chromosome, another one with a Physical Position and the last one is a condition with TRUE or FALSE values. This condition is TRUE when the difference between the i+1 value for Position and i value of Position is not greater than a certain value (in the example is for 1000).
I want to count how many TRUE values there are in a determined range for that Position until a FALSE is found.
As my original data frame is to long I'll leave an example.
CHR <- c(1,1,1,1,2,2,2,3,3,3,3)
POS <- c(10,10000,12000,15000,25,75,50000,50,100,40000,45000)
CONDITION <- c(F,T,T,F,T,F,F,T,F,T,F)
df <- data.frame(CHR,POS,CONDITION)
I want to get something like this:
CHR_r <- c(1,1,2,2,3,3)
from <- c(10,10000,25,50000,50,40000)
to <- c(10,15000,75,50000,100,45000)
count <- c(1,3,2,1,2,2)
result <- data.frame(CHR_r,from,to,count)
Which is the best way to acomplish this? I thought of using a while loop, but I had the problem that it exits the loop each time a FALSE is found. Other thing I thought about was using sequence(rle), but the result was not nearly what I expected, maybe because it's more complicated what I am wanting.