Filtering by pairs

Hi everyone,

My data is as follows:

UID, collection_date,result

The UID is an individual's random ID number, the collection date is the date on which the test was drawn, and the result is, naturally, the result of the test. UID and result are factors, collection_date is a date.

What I'm trying to do is filter to rows where someone changes from a negative to a positive test result. Ex of the data - how would I filter to the two rows with 5, and do this for many such pairings?:

example <- 
cbind(UID = c(1,2,3,4,rep(5,2),6),COLLDT = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 7), RESULT = c("P","P","P","P","N","P","P")

If it is as simple as just two options, you could convert RESULT to an ordered factor and do something like this.


example %>%
  as_tibble() %>%
  mutate(RESULT = factor(RESULT, ordered = TRUE)) %>%
  group_by(UID) %>%
  mutate(mx = max(RESULT), mn = min(RESULT)) %>%
  filter(mx != mn)

And if it matters whether the results were consecutive, you could do something like this.

example %>%
  as_tibble() %>%
  arrange(UID, COLLDT) %>%
  group_by(UID) %>%
  mutate(PRIOR_RES = lag(RESULT))        

Hope this helps!

1 Like

Thank you so much! Your second code chunk is exactly what I've been trying to do.

I tried an overly-complicated approach - because I didn't understand how generally applicable the lag() and lead() functions were.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.