R - starting and resetting a counting column in R based on condition

≡放荡痞女 提交于 2021-02-08 05:45:18

问题


I would like to thank for all the help already in advance, as the help until now has allowed me go get new ideas/hypotheses on how my data could be analysed.

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), vulnerable_period_30count = c(0, 
0, 0, 1, 1, 1, 1, 2, 3, 4, 1, 1, 1, 1, 1, 1, 2, 3, 1, 2, 3, 4, 
1, 2, 3, 4, 1, 1, 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, 0, 0), vulnerable_period_yn = c(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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 0, 0), vulnerable_period_30count_3days = c(0, 
0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 
8, 9, 10, 11, 12, 13, 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, 0, 0), vulnerable_period_3days_yn = c(0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 11, 1, 1, 1, 1, 1, 1, 1, 0, 0)), 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.

The hypothesis is that once a person has had a treatment interruption, he or she is vulnerable for the next 30 days.

So based on these inputs I would like to count/calculate the following outputs:

Output

vulnerable_period_30count: when a treatment interruption occurs (treatment (type) = 0), it start counting to 30. It automatically resets when a treatment interruption occurs.

vulnerable_period_30count_3days: only if a treatment interruption lasts for three days (thus three consecutive 0's) or longer, is the interruption relevant. Then, it will start counting to 30. If the interruption last longer than the three days, it will reset the count again. NOTE: see day 6, the interruption lasts 4 days in total. The count resets when the 4 day interruption ended.

When the count has exceeded 30, it reverts back to 0 and will only start counting when a relevant interruption occurred again.

Here the attempt for the vulnerable_period_30count.

Attempt for vulnerable_period_30count

data$vulnerable_period_30count <- data %>% 
  group_by(record_id, idx = cumsum(`treatment (type)` == 0L)) %>% 
  mutate(counter = row_number()) %>% 
  ungroup %>% 
  select(-idx)
data$test[data$test >30] <- 0

The problem here that it start with 1, 2, 3 already. I hard-stop the count with the last line.

For how to only start count when three or more interruptions occured, i have absolutely no idea.

Hope you can help

BW

Comment OP on Answer 2 @Ekoam Thanks again. The code now runs which is good. Two errors I managed to find. I will use one case to illustrate:

structure(list(record_id = c(110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003), day_count = 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, 60, 61, 62, 63, 64, 65, 66, 67, 
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 
139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 
152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 
165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 
178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 
191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 
204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 
217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 
230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 
243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 
256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 
269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 
282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 
295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 
308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 
321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 
334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 
347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 
360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 
373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 
386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 
399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 
412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 
425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 
438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 
451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 
464, 465, 466, 467, 468, 469, 470, 471, 472), day_count_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, 61, 62, 63, 64, 65, 66, 67, 
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 
139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 
152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 
165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 
178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 
191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 
204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 
217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 
230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 
243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 
256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 
269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 
282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 
295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 
308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 
321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 
334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 
347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 
360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 
373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 
386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 
399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 
412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 
425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 
438, 439, 440, 441, 442, 443, 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 85L, 86L, 87L, 88L, 
89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 
101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 
112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 
123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 
134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 
145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 
156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 
167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 
178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 
189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 
200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 210L, 
211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 219L, 220L, 221L, 
222L, 223L, 224L, 225L, 226L, 227L, 228L, 229L, 230L, 231L, 232L, 
233L, 234L, 235L, 236L, 237L, 238L, 239L, 240L, 241L, 242L, 243L, 
244L, 245L, 246L, 247L, 248L, 249L, 250L, 251L, 252L, 253L, 254L, 
255L, 256L, 257L, 258L, 259L, 260L, 261L, 262L, 263L, 264L, 265L, 
266L, 267L, 268L, 269L, 270L, 271L, 272L, 273L, 274L, 275L, 276L, 
277L, 278L, 279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 
288L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L, 
299L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L, 309L, 
310L, 311L, 312L, 313L, 314L, 315L, 316L, 317L, 318L, 319L, 320L, 
321L, 322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L, 330L, 331L, 
332L, 333L, 334L, 335L, 336L, 337L, 338L, 339L, 340L, 341L, 342L, 
343L, 344L, 345L, 346L, 347L, 348L, 349L, 350L, 351L, 352L, 353L, 
354L, 355L, 356L, 357L, 358L, 359L, 360L, 361L, 362L, 363L, 364L, 
365L, 366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 375L, 
376L, 377L, 378L, 379L, 380L, 381L, 382L, 383L, 384L, 385L, 386L, 
387L, 388L, 389L, 390L, 391L, 392L, 393L, 394L, 395L, 396L, 397L, 
398L, 399L, 400L, 401L, 402L, 403L, 404L, 405L, 406L, 407L, 408L, 
409L, 410L, 411L, 412L, 413L, 414L, 415L, 416L, 417L, 418L, 419L, 
420L, 421L, 422L, 423L, 424L, 425L, 426L, 427L, 428L, 429L, 430L, 
431L, 432L, 433L, 434L, 1L), vulnerable_period_30count_3days = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 
51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 
64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 
77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 
90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 
102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 
113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 
124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 
135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 
146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 
157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 
168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 
179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 189L, 
190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 200L, 
201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 210L, 211L, 
212L, 213L, 214L, 215L, 216L, 217L, 218L, 219L, 220L, 221L, 222L, 
223L, 224L, 225L, 226L, 227L, 228L, 229L, 230L, 231L, 232L, 233L, 
234L, 235L, 236L, 237L, 238L, 239L, 240L, 241L, 242L, 243L, 244L, 
245L, 246L, 247L, 248L, 249L, 250L, 251L, 252L, 253L, 254L, 255L, 
256L, 257L, 258L, 259L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 
267L, 268L, 269L, 270L, 271L, 272L, 273L, 274L, 275L, 276L, 277L, 
278L, 279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 
289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L, 299L, 
300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L, 309L, 310L, 
311L, 312L, 313L, 314L, 315L, 316L, 317L, 318L, 319L, 320L, 321L, 
322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L, 330L, 331L, 332L, 
333L, 334L, 335L, 336L, 337L, 338L, 339L, 340L, 341L, 342L, 343L, 
344L, 345L, 346L, 347L, 348L, 349L, 350L, 351L, 352L, 353L, 354L, 
355L, 356L, 357L, 358L, 359L, 360L, 361L, 362L, 363L, 364L, 365L, 
366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 375L, 376L, 
377L, 378L, 379L, 380L, 381L, 382L, 383L, 384L, 385L, 386L, 387L, 
388L, 389L, 390L, 391L, 392L, 393L, 394L, 395L, 396L, 397L, 398L, 
399L, 400L, 401L, 402L, 403L, 404L, 405L, 406L, 407L, 408L, 409L, 
410L, 411L, 412L, 413L, 414L, 415L, 416L, 417L, 418L, 419L, 420L, 
421L, 422L, 423L, 424L, 425L, 426L, 427L, 428L, 429L, 430L, 431L, 
432L, 433L, 434L, 435L, 1L)), row.names = c(NA, -473L), groups = structure(list(
    record_id = 110003, .rows = structure(list(1:473), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))
  1. At day 66 or day 67 the day count does not stop, and continues counting. This occurs for several other record_id's as wlel.

  2. At day 36 and 38, the vulnerable_day_30_count starts counting too soon.

  3. At the final day, the vulnerable_day_30_count start counting already, although there has not been an interruption of 3 days.


回答1:


Consider a function like this:

# 1. Determine the positions of zeros (p0) in `x`.
# 2. Test whether p0 - 1, p0 - 2, ..., p0 - n + 1 are zeros; 
#    reduce p0 to those that satisfy all conditions.
# 3. Find the upper bounds (ub). For example, if p0 = c(1,2,3), 
#    then upper bounds will be c(2,3,?) (i.e. lead p0). 
#    ? is then 3 + 30 (the max you set) if it is still within 
#    the length of `x`; otherwise is the length of `x` plus 1.
# 4. Compute the sequence between each pair of p0 and ub and 
#    then unlist the result to a vector.
# 5. Assign the vector back to its correct position.

conditional_count <- function(x, n, max = 30L) {
  len <- length(x); out <- integer(len)
  p0 <- which(x == 0L)
  if (n > 1L)
    p0 <- Reduce(function(idx, i)  idx[x[(idx - i + 1L)] == 0L], seq_len(n)[-1L], p0)
  ub <- c(tail(p0, -1L), min(len + 1L, tail(p0, 1L) + max))
  res <- unlist(lapply(ub - p0, seq_len))
  `[<-`(out, p0[[1L]] + seq.int(0L, length(res) - 1L), res)
}

Then you can just

df %>% 
  mutate(
    vulnerable_period_30count = conditional_count(`treatment (type)`, 1L), 
    vulnerable_period_30count_3days = conditional_count(`treatment (type)`, 3L)
  )

Output (the df I used only has four variables: record_id, start, stop, treatment (type). Other variables were removed.)

   record_id start stop treatment (type) vulnerable_period_30count vulnerable_period_30count_3days
1     110101     0    1                1                         0                               0
2     110101     1    2                1                         0                               0
3     110101     2    3                1                         0                               0
4     110101     3    4                0                         1                               0
5     110101     4    5                0                         1                               0
6     110101     5    6                0                         1                               1
7     110101     6    7                0                         1                               1
8     110101     7    8                2                         2                               2
9     110101     8    9                2                         3                               3
10    110101     9   10                2                         4                               4
11    110101    10   11                0                         1                               5
12    110101    11   12                0                         1                               6
13    110101    12   13                0                         1                               1
14    110101    13   14                0                         1                               1
15    110101    14   15                0                         1                               1
16    110101    15   16                0                         1                               1
17    110101    16   17                3                         2                               2
18    110101    17   18                3                         3                               3
19    110101    18   19                0                         1                               4
20    110101    19   20                3                         2                               5
21    110101    20   21                3                         3                               6
22    110101    21   22                3                         4                               7
23    110101    22   23                0                         1                               8
24    110101    23   24                2                         2                               9
25    110101    24   25                2                         3                              10
26    110101    25   26                2                         4                              11
27    110101    26   27                0                         1                              12
28    110101    27   28                0                         1                              13
29    110101    28   29                0                         1                               1
30    110101    29   30                1                         2                               2
31    110101    30   31                1                         3                               3
32    110101    31   32                1                         4                               4
33    110101    32   33                1                         5                               5
34    110101    33   34                1                         6                               6
35    110101    34   35                1                         7                               7
36    110101    35   36                1                         8                               8
37    110101    36   37                1                         9                               9
38    110101    37   38                1                        10                              10
39    110101    38   39                1                        11                              11
40    110101    39   40                1                        12                              12
41    110101    40   41                1                        13                              13
42    110101    41   42                1                        14                              14
43    110101    42   43                1                        15                              15
44    110101    43   44                1                        16                              16
45    110101    44   45                1                        17                              17
46    110101    45   46                1                        18                              18
47    110101    46   47                1                        19                              19
48    110101    47   48                1                        20                              20
49    110101    48   49                1                        21                              21
50    110101    49   50                1                        22                              22
51    110101    50   51                1                        23                              23
52    110101    51   52                1                        24                              24
53    110101    52   53                1                        25                              25
54    110101    53   54                1                        26                              26
55    110101    54   55                1                        27                              27
56    110101    55   56                1                        28                              28
57    110101    56   57                1                        29                              29
58    110101    57   58                1                        30                              30
59    110101    58   59                1                         0                               0
60    110101    59   60                1                         0                               0

Update

conditional_count <- function(x, n, max = 30L) {
  len <- length(x); out <- integer(len)
  p0 <- which(x == 0L)
  if (n > 1L)
    p0 <- Reduce(function(idx, i)  idx[x[(idx - i + 1L)] == 0L], seq_len(n)[-1L], p0)
  if (length(p0) < 1L)
    return(out)
  ub <- c(tail(p0, -1L), min(len + 1L, tail(p0, 1L) + max))
  res <- unlist(lapply(ub - p0, seq_len))
  `[<-`(out, p0[[1L]] + seq.int(0L, length(res) - 1L), res)
}

This error is likely due to no such zero satisfying your conditions stated above. Hence, p0 is of length 0. We can add one more if-else statement to handle this boundary condition. Try with the code shown above. If it's still not working, please consider updating your problem with something like dput(df[record_id == *, ]), where * represents the erratic group (i.e. 110005 in this case).

Update 2

conditional_count <- function(x, n, max = 30L) {
  len <- length(x); out <- integer(len)
  p0 <- which(x == 0L)
  if (n > 1L)
    p0 <- Reduce(function(idx, i) {
      lidx <- idx - i + 1L; lidx <- lidx[lidx > 0L]
      idx[x[lidx] == 0L]
    }, seq_len(n)[-1L], p0)
  if (length(p0) < 1L)
    return(out)
  ub <- c(tail(p0, -1L), min(len + 1L, tail(p0, 1L) + max))
  res <- unlist(lapply(ub - p0, seq_len))
  `[<-`(out, p0[[1L]] + seq.int(0L, length(res) - 1L), res)
}

I managed to reproduce your problem using some simulated data. The error is also caused by a boundary condition. When the code tries to verify the sequence p0 - 1, p0 - 2, ..., it may go into negative if there is a zero at the beginning of the vector treatment (type). I have corrected that part. Try with the updated code above.

Update 3

conditional_count <- function(x, n, max = 30L) {
  len <- length(x); out <- integer(len)
  p0 <- which(x == 0L)
  if (n > 1L)
    p0 <- Reduce(function(idx, i) {
      lidx <- idx - i + 1L
      idx <- idx[lidx > 0L]; lidx <- lidx[lidx > 0L]
      idx[x[lidx] == 0L]
    }, seq_len(n)[-1L], p0)
  if (length(p0) < 1L)
    return(out)
  ub <- pmin(c(tail(p0, -1L), len), p0 + max - 1L)
  rl <- ub - p0 + 1L
  res <- unlist(lapply(rl, seq_len))
  pos <- inverse.rle(list(lengths = rl, values = p0)) + res - 1L
  `[<-`(out, pos, res)
}

Try with this new one. I reworked some logics. I previously assumed that only the count for the last valid zero can go beyond 30 days. That's why the count never stops for in-between valid zeros. Furthermore, the resultant vector was not assigned to the correct position. This bug also creates some anomalies you have observed. I have corrected both. The result now looks like this

structure(list(record_id = c(110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003, 110003, 110003, 110003, 
110003, 110003, 110003, 110003, 110003), day_count = 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, 60, 61, 62, 63, 64, 65, 66, 67, 
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 
139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 
152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 
165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 
178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 
191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 
204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 
217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 
230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 
243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 
256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 
269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 
282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 
295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 
308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 
321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 
334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 
347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 
360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 
373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 
386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 
399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 
412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 
425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 
438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 
451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 
464, 465, 466, 467, 468, 469, 470, 471, 472), day_count_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, 61, 62, 63, 64, 65, 66, 67, 
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 
139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 
152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 
165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 
178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 
191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 
204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 
217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 
230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 
243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 
256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 
269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 
282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 
295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 
308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 
321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 
334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 
347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 
360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 
373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 
386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 
399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 
412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 
425, 426, 427,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0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(NA, 
-473L), groups = structure(list(record_id = 110003, .rows = structure(list(
    1:473), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
"list"))), row.names = 1L, class = c("tbl_df", "tbl", "data.frame"
), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
))


来源:https://stackoverflow.com/questions/64621711/r-starting-and-resetting-a-counting-column-in-r-based-on-condition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!