I have a data set like below. Each patient has 3 visits and they can transition between the 3 states from visit to visit.
ID <- c(1,1,1,2,2,2,3,3,3)
Visit <- c(1,2,3,1,2,3,1,2,3)
State <- c(2,1,1,3,2,1,2,3,1)
I want to make a data frame that count the number of transitions of states from visit 1 to visit 2. For Visit 1 to Visit 2, the matrix will be like: (the rows represent the state at visit 1, and the columns represent the state at visit 2. Entries on the diagonals
represent counts of participants who did not transition)