我有一个如下所示的数据集。每个患者有 3 次就诊,他们可以在每次就诊的 3 种状态之间转换。
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)
我想制作一个数据框来计算从访问 1 到访问 2 的状态转换次数。对于访问 1 到访问 2,矩阵将类似于:(行代表访问 1 时的状态,列代表访问 1 时的状态)访问时的状态 2. 对角线上的条目 代表未转换的参与者数量)
A
tidyverse
方法:
data.frame(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)) |>
# identify the next State within each ID
mutate(next_State = lead(State), .by = ID) |>
# we only want when Visit is 1
filter(Visit == 1) |>
# How many of each State / next_State?
count(State, next_State) |>
# add in any missing combinations with n = 0
complete(State = 1:3, next_State = 1:3, fill = list(n=0)) |>
# reshape wide
pivot_wider(names_from = next_State, values_from = n)
结果
# A tibble: 3 × 4
State `1` `2` `3`
<dbl> <int> <int> <int>
1 1 0 0 0
2 2 1 0 1
3 3 0 1 0