I'm trying to subset a grouped data set. Although there are many questions (e.g. Select the first and last row by group in a data frame) on this subject, none of them fit into my case described here. An example data is
df<-data.frame("id"=c(1,1,1,2,2,2,3,3,3),
"x1"=c(NA,1,1,1,1,1,0,0,1),
"x2"=c(10,8,13,4,7,6,9,10,6))
I want to retain data for cases where "x1" is first seen to be equal to 1 for each id. I expect to have
df<-data.frame("id"=c(1,2,3),
"x1"=c(1,1,1),
"x2"=c(8,4,6))
I tried
df<-df %>%
group_by(id) %>%
filter(first(x1)==1)
but it provides undesired output. Any help on this is greatly appreciated.