Similar to Extract rows for the first occurrence of a variable in a data frame Dataset example:
ID grade date
A 1 1/1/2001
A 3 1/2/2002
A 5 2/4/2002
B 4 1/1/2008
B 4 1/1/2010
B 5 1/1/2011
B 5 1/3/2011
B 4 1/5/2011
C 2 1/1/2006
C 5 1/1/2007
C 5 1/1/2008
D 3 1/1/1996
D 5 1/1/1997
D 5 1/1/1999
E 1 1/1/2003
E 3 1/1/2005
E 3 1/1/2007
I would like to keep all entries per person until they hit grade 5 and then stop irrespective of grades after the earliest mention of 5. Not all IDs have reached stage 5, they should remain untouched even if they have multiple of the same grade
Desired output:
ID grade date
A 1 1/1/2001
A 3 1/2/2002
A 5 2/4/2002
B 4 1/1/2008
B 4 1/1/2010
B 5 1/1/2011
C 2 1/1/2006
C 5 1/1/2007
D 3 1/1/1996
D 5 1/1/1997
E 1 1/1/2003
E 3 1/1/2005
E 3 1/1/2007
I can used order and in dplyr the filter function but I am unsure how to apply these to just the 5's per person and then exclude anything afterwards?
Many thanks