I had written some code months ago to find the days until the next visit and the days since the last visit to track patient progress and identify missed appointments. This code worked beautifully and gave the correct number of days (I verified a sample by hand) but now it isn't working. It appears as though the "Group_by()" command is being ignored and it is simply calculating the number of days between the observations as they are ordered in R rather than giving an "NA" at the beginning and end of each grouping. I guess I'm mostly just confused why this was working but then stopped.
Code:
dd5 <- dd5 %>%
arrange(PatientID, GroupingUid, VisitDate) %>%
group_by(GroupingUid) %>%
mutate(Days.Until.Next.Visit = as.numeric(lead(VisitDate) - VisitDate))
dd5 <- dd5 %>%
arrange(PatientID, GroupingUid, VisitDate) %>%
group_by(GroupingUid) %>%
mutate(Days.Since.Last.Visit = as.numeric(VisitDate - lag(VisitDate)))
UPDATE: I have tried ungrouping and then arranging and regrouping, I tried grouping and then arranging, and I have removed the as.numeric() but not had any success.
GroupingUID and PatientUID are unique identifiers that I am using to track each patient/injury. Each combo of GroupingUID and PatientID should have the days calculated separately. VisitDate is obviously the date.
83515888-E9C2-4B71-87EB-E954182DED88 1 8/22/2017 38 5
86169252-F2CD-4EDF-843F-6946AD45376A 1 9/29/2017 -56 38
B8498C0E-CF25-4F05-A7AC-DFECE1252183 1 8/4/2017 25 -56
BEABCA88-E3EB-4D48-B52B-D5639C527141 1 8/29/2017 2 25
DE7493C0-F72C-44D8-8469-9AA30662769D 1 8/31/2017 -2 2
EE720E94-9B1F-4350-A1B5-87E71BF40E35 1 8/29/2017 -32 -2
3970FF5F-4FEB-4509-A94E-0678000C505A 10 7/28/2017 0 -32
461BC1C4-F07C-4F42-BD8E-7545644FF4BA 10 7/28/2017 54 0
0656001E-2289-4B5D-B49A-CB6F57BD8E89 100 5/8/2018 7 13
0656001E-2289-4B5D-B49A-CB6F57BD8E89 100 5/15/2018 7 7
0656001E-2289-4B5D-B49A-CB6F57BD8E89 100 5/22/2018 -33 7
0286CDBF-1F52-4074-94B6-AE541E0DC9BF 10006 4/19/2018 6 -33
0286CDBF-1F52-4074-94B6-AE541E0DC9BF 10006 4/25/2018 8 6
0286CDBF-1F52-4074-94B6-AE541E0DC9BF 10006 7/12/2018 4 7
0286CDBF-1F52-4074-94B6-AE541E0DC9BF 10006 7/16/2018 -88 4
04A80019-561F-4CD4-B2D5-CAE059051750 10006 4/19/2018 6 -88
04A80019-561F-4CD4-B2D5-CAE059051750 10006 4/25/2018 8 6
F75FC4A1-587A-414E-BA35-9E5A6948F3FC 10006 6/28/2018 7 7
F75FC4A1-587A-414E-BA35-9E5A6948F3FC 10006 7/5/2018 7 7
F75FC4A1-587A-414E-BA35-9E5A6948F3FC 10006 7/12/2018 -300 7
09772AD1-2096-4C56-83AB-08CF9876ADDC 102 9/15/2017 7 -300
87C3C708-64F6-4321-9D98-F258B04B7E39 102 10/27/2017 7 7
87C3C708-64F6-4321-9D98-F258B04B7E39 102 11/3/2017 -49 7
BE2EFB0A-A1D8-49E6-88CF-D9818D1EF60D 102 9/15/2017 7 -49
BE2EFB0A-A1D8-49E6-88CF-D9818D1EF60D 102 9/22/2017 7 7
BE2EFB0A-A1D8-49E6-88CF-D9818D1EF60D 102 9/29/2017 14 7
BE2EFB0A-A1D8-49E6-88CF-D9818D1EF60D 102 10/13/2017 7 14
BE2EFB0A-A1D8-49E6-88CF-D9818D1EF60D 102 10/20/2017 7 7
BE2EFB0A-A1D8-49E6-88CF-D9818D1EF60D 102 10/27/2017 7 7
BE2EFB0A-A1D8-49E6-88CF-D9818D1EF60D 102 11/3/2017 -37 7
0B8DAD9F-2141-42A4-BBC8-F026DA4CB24A 103 9/27/2017 0 -37
440B3D2F-7E4E-42AE-9274-CCF751A10A94 103 9/27/2017 0 0
70112B85-7396-4A72-8853-B308D061383C 103 9/27/2017 0 0
889AB652-42C6-409F-A389-1CA96E66997B 103 9/27/2017 0 0
AAD6D986-80FE-4143-8ABF-BCADE73AB2EA 103 9/27/2017 0 0
C03778F8-1499-4FB0-99CC-D3A3B1143E4F 103 9/27/2017 0 0
C7E21544-96F4-46CB-87F5-11D882352FE0 103 9/27/2017 0 0
DD401067-3FCA-4828-8765-635E82181D1E 103 9/27/2017 0 0
F873284F-B86E-47CA-BA68-3AD4650CD72F 103 9/27/2017 -6 0
0B1CC77A-BADC-4971-AB14-7A5FB982DC4C 104 9/21/2017 7 -6
0B1CC77A-BADC-4971-AB14-7A5FB982DC4C 104 9/28/2017 14 7
0B1CC77A-BADC-4971-AB14-7A5FB982DC4C 104 10/12/2017 0 14
352F0DFE-C06F-46CC-AFE5-F6B8929FB31B 104 10/12/2017 -14 0