Suppose I have a dataframe like,
library(dplyr)
data <- tibble(
label = c("a","a","b","a","c","c","a")
)
data$index <- 1:nrow(data)
I don't want to subset all the rows where label == "a", but only the first rows where this is true.
In the example, I would want the first two rows :
label index
<chr> <int>
1 a 1
2 a 2
because the next row the label is "b". All subsequent rows where label == "a" should be ignored.
I have implemented an ugly solution with a for loop, but surely there is an efficient way to filter like this?