Below, the data has already been reshaped, and the inputs and expected output are listed.
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"))
Explanation
Input
start and stop are the day-count. The daily treatment is listed in treatment, with 0 = no treatment, which is an interruption, and 1:3 are treatment A/B/C.
Output
Based on the treatment column, I want to count per day:
n_interruption_periods: the sum/number of interruption periods, irrespective of the duration of the interruptionn_interruption_periods_3days: sum/the number of interruptions, with a condition that you should only count when the duration was >= 3 days. Interruptions shorter than 3 day are not of interestn_interruption_days_3days: the cumulative sum/number of interruption days, where interruptions are only counted from day 3 of the interruption and on.
Question
I want to create a script which calculates these abovementioned output variables automatically based on the treatment variable.
Hope you can help
BW
Response OP
Here is a part of the data which illustrate the problem:
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"))
With the suggested code, there are two issues:
I believe the resultant vector was not assigned to the correct position. Here you can see that 110002 first data on
n_interruption_periodsandn_interruption_periods_3daysare extended from 110001 results.When I try to run the third vector, I receive this error: Error in while (any(d != 0)) { : missing value where TRUE/FALSE needed
BW