Created
June 30, 2018 20:29
-
-
Save mcfrank/8c2901924bd7e8d9ea20361608d62b25 to your computer and use it in GitHub Desktop.
transition between states in some code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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