Here's a sample of booleans I have as part of a data.frame:

atest <- c(FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE)

I want to return a sequence of numbers starting at 1 from each FALSE and increasing by 1 until the next FALSE.

The resulting desired vector is:

[1]  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 10  1

Here's the code that accomplishes this, but I'm sure there's a simpler or more elegant way to do this in R. I'm always trying to learn how to code things more efficiently in R rather than simply getting the job done.

result <- c()
x <- 1
for(i in 1:length(atest)){
    if(atest[i] == FALSE){
        result[i] <- 1
        x <- 1
    } 
    if(atest[i] != FALSE){
        x <- x+1
         result[i] <- x
    }
}
r
Edit
Report