I have data grouped using dplyr in R. I would like to find the 'date' after the last occurrence of observations ('B') equal to or greater than 1 (1, 2, 3 or 4) in each group ('A'). In other words, the 'date' where 1/2/3/4 has turned to 0.
Simply finding the date for the first occurrence of 0 will not work as in some groups 1/2/3/4 switches to 0 and then back again and does not give the result I'd like.
I would like this 'date' for each group to be given in a new column ('date.after').
For example, given the following sample of data, grouped by A (this has been simplified, my data is actually grouped by 3 variables):
A B date
a 2 1
a 2 2
a 1 5
a 0 8
b 3 1
b 3 4
b 3 6
b 0 7
b 0 9
c 1 2
c 1 3
c 1 4
I would like to achieve the following:
A B date date.after
a 2 1 8
a 2 2 8
a 1 5 8
a 0 8 8
b 3 1 7
b 3 4 7
b 3 6 7
b 0 7 7
b 0 9 7
c 1 2 NA
c 1 3 NA
c 1 4 NA
I hope this makes sense, thank you all very much for your help! This post may look familiar, I have just asked a very similar question:
How to find the last occurrence of a certain observation in grouped data in R?