I have the results of a test taken by a number of individuals at as many as 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 looks like:
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'd like to add an identifier to each test to note whether that test was a conversion from the previous status (negative to positive), a reversion (positive to negative), or stable. The catch is that I'm not just comparing the base test to the six months test, six months to twelve months, etc. - in cases like C, the sixmos test should be marked as stable or inconclusive (the exact term for that is ambiguous), and (more importantly) the twelvemos test should then be compared to the base test and marked as a reversion. Conversely, if someone had a sequence of "Negative", "Indeterminate", "Negative", that should be stable.
It's the latter part that I'm stuck on; if it were just a sequence of comparisons per participant, I'd be all right, but I'm having trouble thinking about how to elegantly deal with these variable comparison pairs. Your help is, as always, much appreciated.