I have a data set of different patient ID's, clinic visit dates, and attendance (see example data below, separated by patient ID for clarity).
I am interested in sequentially counting treatment episodes, which are defined as attending >= 4 visits for their starting month, followed by >= 1 visit every month afterwards. If a patient attends <1 visit after starting (i.e., after completing their initial >=4 visits in the starting month), that treatment episode is considered ended. A new treatment episode subsequently starts the next time a patient attends >= 4 visits in a given month, and that same episode continues as long as the patient attends >=1 visit/month thereafter. When patients either do not meet or break this pattern, I'd like to input 0.
Example data (note: I've excluded each day's date to prevent the example from being excessively long and re-produced dates to give a clearer picture of the desired data):
Patient ID | Visit Date | Attendance |
---|---|---|
1 | 01/01/2023 | Yes |
1 | 01/02/2023 | Yes |
1 | 01/03/2023 | Yes |
1 | 01/04/2023 | Yes |
1 | 02/01/2023 | Yes |
1 | 03/01/2023 | Yes |
1 | 04/01/2023 | No |
1 | 05/01/2023 | Yes |
1 | 06/01/2023 | No |
1 | 07/01/2023 | Yes |
1 | 07/02/2023 | Yes |
1 | 07/03/2023 | Yes |
1 | 07/04/2023 | Yes |
1 | 08/01/2023 | Yes |
---------- | ---------- | ---------- |
Patient ID | Visit Date | Attendance |
---------- | ---------- | ---------- |
2 | 01/01/2023 | Yes |
2 | 02/01/2023 | Yes |
2 | 03/01/2023 | Yes |
2 | 03/02/2023 | Yes |
2 | 03/03/2023 | Yes |
2 | 03/04/2023 | Yes |
2 | 04/01/2023 | Yes |
2 | 05/01/2023 | Yes |
2 | 07/01/2023 | Yes |
Desired data:
Patient ID | Visit Date | Attendance | Tx Episode |
---|---|---|---|
1 | 01/01/2023 | Yes | 1 |
1 | 01/02/2023 | Yes | 1 |
1 | 01/03/2023 | Yes | 1 |
1 | 01/04/2023 | Yes | 1 |
1 | 02/01/2023 | Yes | 1 |
1 | 03/01/2023 | Yes | 1 |
1 | 04/01/2023 | No | 0 |
1 | 05/01/2023 | Yes | 0 |
1 | 06/01/2023 | No | 0 |
1 | 07/01/2023 | Yes | 2 |
1 | 07/02/2023 | Yes | 2 |
1 | 07/03/2023 | Yes | 2 |
1 | 07/04/2023 | Yes | 2 |
1 | 08/01/2023 | Yes | 2 |
---------- | ---------- | ---------- | ---------- |
Patient ID | Visit Date | Attendance | Tx Episode |
---------- | ---------- | ---------- | ---------- |
2 | 01/01/2023 | Yes | 0 |
2 | 02/01/2023 | Yes | 0 |
2 | 03/01/2023 | Yes | 1 |
2 | 03/02/2023 | Yes | 1 |
2 | 03/03/2023 | Yes | 1 |
2 | 03/04/2023 | Yes | 1 |
2 | 04/01/2023 | Yes | 1 |
2 | 05/01/2023 | Yes | 1 |
2 | 07/01/2023 | Yes | 0 |
I am somewhat new to programming in R and have initially attempted to use ifelse() but wasn't able to come up with logicals that worked. I've also attempted to write loops, which have failed to run.
Any help would be greatly appreciated and I'm happy to provide more detail if the above isn't clear.
Thanks in advance for your time/effort!