Skip to content

Instantly share code, notes, and snippets.

@mcfrank
Created June 30, 2018 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcfrank/8c2901924bd7e8d9ea20361608d62b25 to your computer and use it in GitHub Desktop.
Save mcfrank/8c2901924bd7e8d9ea20361608d62b25 to your computer and use it in GitHub Desktop.
transition between states in some code
library(tidyverse)
foo <- data_frame(time = c(1:5, 1:5),
code = c(1,2,3,2,1,0,0,1,2,1),
subid = c(rep(1,5), rep(2, 5)))
foo %>%
group_by(subid) %>%
mutate(d_code = c(0,diff(code)),
one_two_transition = code == 2 & d_code == 1,
two_three_transition = code == 3 & d_code == 1) %>%
summarise(one_two = sum(one_two_transition),
two_three = sum(two_three_transition))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment