我有一个很简单的问题:想象一个认知测试有6个项目,每个项目都在0-10量表上单独评分,现在假设5名患者在一天中的3个(或更多)不同时间进行了该测试。
df <- tibble(
id = rep(1:5, each = 3),
time = rep(c("morning", "evening", "night"), each = 1, times = 5),
i1 = round(runif(15, 0, 10)),
i2 = round(runif(15, 0, 10)),
i3 = round(runif(15, 0, 10)),
i4 = round(runif(15, 0, 10)),
i5 = round(runif(15, 0, 10)),
i6 = round(runif(15, 0, 10))
)
现在,我如何计算每位患者在一天中不同时间的每个单项评分变化?tidyverse
方法非常受欢迎。
P.S:输出最好是这样的:
df <- tibble(
id = rep(1:5, each = 3),
time = rep(c("morning", "evening", "night"), each = 1, times = 5),
i1 = round(runif(15, 0, 10)),
i2 = round(runif(15, 0, 10)),
i3 = round(runif(15, 0, 10)),
i4 = round(runif(15, 0, 10)),
i5 = round(runif(15, 0, 10)),
i6 = round(runif(15, 0, 10)),
diff_morning_evening = rep(NA, 15),
diff_morning_night = rep(NA, 15),
diff_evening_night = rep(NA, 15),
)
2条答案
按热度按时间nnsrf1az1#
以简洁方式计算变化的方法:
特殊行求和法
gxwragnw2#
您可以使用
combn
生成2次的所有组合,并通过设置FUN = diff
将diff
应用于每个组合以计算变化。如果需要宽格式输出,请将上面的代码传递给
tidyr::pivot_wider()
: