I have the results of a test conducted by several people over four time periods. Here's a sample:
dat <- structure(list(Participant_ID = c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C"), phase = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("base", "sixmos", "twelvemos", "eighteenmos"), class = "factor"), result = c("Negative", "Negative", "Negative", "Negative", "Negative", "Positive", "Negative", NA, "Positive", "Indeterminate", "Negative", "Negative")), .Names = c("Participant_ID", "phase", "result"), row.names = c(1L, 2L, 3L, 4L, 97L, 98L, 99L, 100L, 9L, 10L, 11L, 12L), class = c("cast_df", "data.frame"))
which is as follows:
Participant_ID phase result 1 A base Negative 2 A sixmos Negative 3 A twelvemos Negative 4 A eighteenmos Negative 97 B base Negative 98 B sixmos Positive 99 B twelvemos Negative 100 B eighteenmos <NA> 9 C base Positive 10 C sixmos Indeterminate 11 C twelvemos Negative 12 C eighteenmos Negative
I would like to add an identifier to each test to indicate whether this test was a conversion from a previous state (from negative to positive), deviation (positive to negative), or stable. The trick is that I'm not just comparing a basic test with a six-month test, from six months to twelve months, etc. - in cases like C, a test for six months should be marked as stable or inconclusive (the exact term is ambiguous for this) and (more importantly) the twelvemos test should then be compared with the base test and marked as a reversal. And vice versa, if someone had a sequence of "Negative", "Indefinite", "Negative", this should be stable.
This is the last part I'm stuck on; if it were just a sequence of comparisons for each participant, I would be fine, but I had trouble thinking about how to elegantly handle these pairs of variable comparisons. Your help, as always, is greatly appreciated.