下面,数据已被重新调整,并列出了输入和预期输出。
Data
structure(list(record_id = c(110101, 110101, 110101, 110101,
110101, 110101, 110101, 110101, 110101, 110101, 110101, 110101,
110101, 110101, 110101, 110101, 110101, 110101, 110101, 110101,
110101, 110101, 110101, 110101, 110101, 110101, 110101, 110101,
110101, 110101, 110101, 110101, 110101, 110101, 110101, 110101,
110101, 110101, 110101, 110101, 110101, 110101, 110101, 110101,
110101, 110101, 110101, 110101, 110101, 110101, 110101, 110101,
110101, 110101, 110101, 110101, 110101, 110101, 110101, 110101
), start = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59), stop = c(1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60), `treatment (type)` = c(1,
1, 1, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 3, 3, 0, 3, 3, 3,
0, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), n_interruption_periods = c(0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), n_interruption_periods_3days = c(0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), n_interruption_days_3days = c(0,
0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7)), row.names = c(NA,
-60L), class = c("tbl_df", "tbl", "data.frame"))
解释
Input
start
and stop
是天数。日常治疗方法列于treatment
,其中 0 = 没有治疗,即中断,1:3 是治疗 A/B/C。
Output基于treatment
列,我想计算每天:
-
n_interruption_periods
:中断周期的总和/数量,与中断的持续时间无关
-
n_interruption_periods_3days
:总和/中断次数,条件是仅在持续时间 >= 3 天时才进行计数。少于 3 天的中断不值得关注
-
n_interruption_days_3days
:中断天数的累计总和/数量,其中中断仅从中断的第 3 天起计算。
Question我想创建一个脚本,根据以下内容自动计算上述输出变量treatment
多变的。
希望你能帮忙
BW
响应操作
这是说明问题的部分数据:
structure(list(record_id = c(110001, 110002, 110002, 110002,
110001), day_count = c(732, 0, 1, 2, 733), day_count_stop = c(733,
1, 2, 3, 734), oac_class = c(0, 1, 1, 1, 1), n_interruption_periods = c(1,
1, 0, 0, 1), n_interruption_periods_3days = c(1, 1, 0, 0, 1)), row.names = c(NA,
-5L), groups = structure(list(record_id = c(110001, 110002),
.rows = structure(list(c(1L, 5L), 2:4), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
使用建议的代码,存在两个问题:
-
我相信生成的向量没有分配到正确的位置。在这里你可以看到 110002 第一个数据n_interruption_periods
and n_interruption_periods_3days
从 110001 个结果扩展而来。
-
当我尝试运行第三个向量时,收到此错误:
while (any(d != 0)) { 中的错误:缺少 TRUE/FALSE 需要的值
BW