The data frame below is grouped by id:
id<- c(1,1,1,2,3,3,4,4,4,5,5,6)
x <- c(0,1,0,0,1,1,0,0,1,0,0,1)
df <- data.frame(id, x)
I am looking for a way to filter the data in R based a condition. The condition is; if an id includes 1 in column x delete the preceding rows containing 0 for that id while maintaining the other structure of the data. The expected output is
> df
id x
1 1
1 0
2 0
3 1
3 1
4 1
5 0
5 0
6 1
I tried to subset the data using the filter function in the dplyr package as in the code below:
df <- df %>%
group_by(id) %>%
filter(first(x)==1 | x == 0)
but I am not getting the expected and I am reaching out for help. I greatly appreciate any help.
xis binary as in your example, you can dodf |> filter(cumsum(x) > 0 | sum(x) == 0, .by = id).