I am trying to produce a Sankey plot of the events that take one week before and one week after an index event of a patient. Imagine I have the following data frame:
df =
patient_id start_date end_date Value Index_event_date Value_Index_event
1 28-12-1999 02-01-2000 A 01-01-2000 X
2 28-12-2000 02-12-2001 B 01-01-2001 X
3 28-12-2001 02-01-2002 A 01-01-2002 X
I would like to group into "codes" the above data frame. For example, one week before the index event is code1, the week of the index event is code2, and the next week after the index event is code3.
The resulting data frame would be:
patient_id code1 code2 code3
1 A X A
2 B X Na
3 A X A
In the above example all patients except for patient 2 have observations in both weeks (one before and one after the index event). In the case of patient 2, it has only an observation in the week before the index event and that is why for code3 (week after the index event), we see an Na.