I try to filter only the first type=="y" value observed.
df<-data.frame(id=c(1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,3,3),
type=c("x","x","y","x","x","x","x","y","y","x","x","x","x","y","x","y","x","x"))
Desired output:
id type
1 x
1 x
1 y
2 x
2 x
2 x
2 y
3 x
3 x
3 x
3 x
3 y
I try it with the code:
library(dplyr)
df %>% filter(type == "x"|sum(type == "y") == 1) %>%
ungroup