I'm looking at constructing a recurrent event survival analysis, and am struggling to work out how to calculate time intervals between events. My data is long format, with each row indicating a hospital episode and the age (in months) at the episode. My issue is that for the analysis I need to calculate time between episodes for each participant. I realize that this will probably involve simply looping/iterating across participants, but I can't figure out how to get the time between event n and n-1 within each participant.
I've found a previous question Date-time differences between rows in R that partly answers my question, but it doesn't give me any ideas of how to implement this for multiple events, for each participant, where the number of events differs per participant.
[ID] [age_of_hosp]
[1,] 3600001 872
[2,] 3600001 874
[3,] 3600001 868
[4,] 3600001 882
[5,] 3600001 873
[6,] 3600001 870
[7,] 3600001 869
[8,] 3600001 562
[9,] 3600001 871
[10,] 3600001 873
[11,] 3600001 885
[12,] 3600001 868
[13,] 3600001 852
[14,] 3600001 887
[15,] 3600001 885
[16,] 3600001 887
[17,] 3600001 853
[18,] 3600001 617
[19,] 3600001 885
[20,] 3600001 874
[21,] 3600001 617
[22,] 3600001 871
[23,] 3600001 851
[24,] 3600002 NA
[25,] 3600003 NA
[26,] 3600004 865
[27,] 3600005 655
[28,] 3600005 667
[29,] 3600005 656
[30,] 3600005 664
[31,] 3600006 814
[32,] 3600006 821
[33,] 3600006 821
[34,] 3600006 755
[35,] 3600006 813
Any advice or pointers would be great!