Suppose we start with a data frame that looks like the below:
ID STATE_1 STATE_2
1 1 NULL NULL
2 1 FRY NULL
3 1 NULL CRY
4 1 FRY CRY
5 1 NULL NULL
6 1 NULL NULL
7 1 FRY CRY
8 1 NULL NULL
9 5 NULL NULL
10 5 NULL CRY
11 5 FRY NULL
12 5 NULL NULL
Generated as follows for those who would like to replicate:
ID <- c(1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 5)
STATE_1 <- c("NULL", "FRY", "NULL", "FRY", "NULL", "NULL", "FRY", "NULL", "NULL", "NULL", "FRY", "NULL")
STATE_2 <- c("NULL", "NULL", "CRY", "CRY", "NULL", "NULL", "CRY", "NULL", "NULL", "CRY", "NULL", "NULL")
df <- data.frame(ID, STATE_1, STATE_2)
How would one add a FLAG column, whereby for each ID, once one of first of multiple states is triggered by an element that is not NULL, then all subsequent rows for that ID are flagged with that first non-NULL status? So for example, with the FLAG column added, the output would look like this:
ID STATE_1 STATE_2 FLAG [Explanations for FLAG column elements]
1 1 NULL NULL NULL No flag tripped for ID 1 (yet) so value is NULL
2 1 FRY NULL FRY FRY tripped in STATE_1 so FRY applies for all subsequent rows for this ID
3 1 NULL CRY FRY Ignore CRY since FRY happened first
4 1 FRY CRY FRY
5 1 NULL NULL FRY
6 1 NULL NULL FRY
7 1 FRY CRY FRY
8 1 NULL NULL FRY
9 5 NULL NULL NULL No flag tripped for ID 5 (yet) so value is NULL
10 5 NULL CRY CRY CRY tripped in STATE_2 so CRY applies for all subsequent rows for this ID
11 5 FRY NULL CRY Ignore FRY since CRY happened first
12 5 NULL NULL CRY But please read the following paragraph, because another possible scenario is omitted for sake of brevity
If in row 10 above, for ID 5, there had both appeared FRY and CRY in that same row, then FRY would have governed for all subsequent ID 5 rows. I didn't include this scenario in my example above. So there is a priority where STATE_1 trumps STATE_2, etc.
In my actual data there are 6 states, compared with the 2 states (STATE_1 and STATE_2) in this example.
I have a strong preference for working with dplyr().
As I researched this, there are several posts addressing flagging data based on a numeric trigger, but I couldn“t find any solutions using character string triggers like in my data. I am hoping there is a character string solution so I can avoid the added steps of converting from character strings to numeric variables.