Filtering, subsetting, and matching data from two separate data frames based on the ID and the period between the dates in R

Solution 1:

One approach is to use the fuzzyjoin package. This will allow you to join the two data frames where the date falls between admission/discharge date, and Patient.ID matches.

After arrange and sorting the Test.date you can slice and take the first value which should be the lowest date within that patient's hospitalization. You can also use slice_min(Test.date) without arrange.

library(tidyverse)
library(lubridate)
library(fuzzyjoin)

df1$Admission.date <- mdy_hm(df1$Admission.date)
df1$Discharge.date <- mdy_hm(df1$Discharge.date)
df2$Test.date <- mdy_hm(df2$Test.date)

fuzzy_inner_join(df1, 
                 df2, 
                 by = c("Patient_ID",  "Admission.date" = "Test.date", "Discharge.date" = "Test.date"),
                 match_fun = c(`==`, `<=`, `>=`)) %>%
  group_by(Patient_ID.x, Admission.date, Discharge.date) %>%
  arrange(Test.date) %>%
  slice(1)

Output

     Row Date1      Time1    Date2      Time2    Patient_ID.x Admission.date      Discharge.date      Test_date  Test_time Value Patient_ID.y Test.date          
   <int> <chr>      <chr>    <chr>      <chr>           <int> <dttm>              <dttm>              <chr>      <chr>     <dbl>        <int> <dttm>             
 1     2 2019-08-22 16:15:00 2019-09-12 12:33:00            3 2019-08-22 16:15:00 2019-09-12 12:33:00 2019-09-06 10:13:00   2.04            3 2019-09-06 10:13:00
 2    23 2019-08-30 12:40:00 2019-09-23 12:20:00           33 2019-08-30 12:40:00 2019-09-23 12:20:00 2019-09-03 10:29:00   3.54           33 2019-09-03 10:29:00
 3    59 2019-09-14 11:35:00 2019-10-15 13:20:00           44 2019-09-14 11:35:00 2019-10-15 13:20:00 2019-09-17 10:55:00   1.92           44 2019-09-17 10:55:00
 4    54 2019-09-13 16:55:00 2019-09-27 08:09:00           94 2019-09-13 16:55:00 2019-09-27 08:09:00 2019-09-17 10:55:00   1.55           94 2019-09-17 10:55:00
 5    93 2019-09-29 16:45:00 2019-10-23 13:30:00           94 2019-09-29 16:45:00 2019-10-23 13:30:00 2019-10-11 11:28:00   1.48           94 2019-10-11 11:28:00
 6     8 2019-08-26 15:50:00 2019-10-29 13:47:00          161 2019-08-26 15:50:00 2019-10-29 13:47:00 2019-10-09 11:19:00   3.55          161 2019-10-09 11:19:00
 7     6 2019-08-24 12:30:00 2019-09-19 12:11:00          268 2019-08-24 12:30:00 2019-09-19 12:11:00 2019-09-13 10:43:00   3.48          268 2019-09-13 10:43:00
 8    62 2019-09-15 11:50:00 2019-11-26 09:48:00          274 2019-09-15 11:50:00 2019-11-26 09:48:00 2019-09-17 11:09:00   3.54          274 2019-09-17 11:09:00
 9    46 2019-09-10 14:15:00 2019-10-16 14:30:00          279 2019-09-10 14:15:00 2019-10-16 14:30:00 2019-09-12 10:17:00   3.51          279 2019-09-12 10:17:00
10    45 2019-09-10 12:00:00 2019-10-16 10:15:00          409 2019-09-10 12:00:00 2019-10-16 10:15:00 2019-09-16 10:16:00   1.9           409 2019-09-16 10:16:00
11    67 2019-09-17 23:55:00 2019-12-03 11:50:00          443 2019-09-17 23:55:00 2019-12-03 11:50:00 2019-10-21 10:37:00   3.28          443 2019-10-21 10:37:00
12    60 2019-09-14 16:30:00 2019-09-30 12:24:00          473 2019-09-14 16:30:00 2019-09-30 12:24:00 2019-09-18 10:25:00   2.01          473 2019-09-18 10:25:00
13    87 2019-09-25 16:15:00 2019-11-21 14:44:00          510 2019-09-25 16:15:00 2019-11-21 14:44:00 2019-09-27 10:43:00   1.35          510 2019-09-27 10:43:00
14    32 2019-09-03 11:15:00 2019-10-22 10:00:00          519 2019-09-03 11:15:00 2019-10-22 10:00:00 2019-09-13 10:43:00   4.46          519 2019-09-13 10:43:00
15    21 2019-08-29 19:30:00 2019-09-25 12:30:00          599 2019-08-29 19:30:00 2019-09-25 12:30:00 2019-09-04 10:25:00   6.05          599 2019-09-04 10:25:00
16    12 2019-08-26 21:10:00 2020-01-10 14:30:00          702 2019-08-26 21:10:00 2020-01-10 14:30:00 2019-09-16 10:16:00   1.5           702 2019-09-16 10:16:00
17    30 2019-09-02 18:20:00 2019-09-20 16:10:00          766 2019-09-02 18:20:00 2019-09-20 16:10:00 2019-09-04 10:26:00   4.15          766 2019-09-04 10:26:00
18   101 2019-10-01 19:10:00 2019-10-17 13:40:00          935 2019-10-01 19:10:00 2019-10-17 13:40:00 2019-10-04 11:02:00   3.92          935 2019-10-04 11:02:00
19    88 2019-09-25 21:30:00 2019-11-08 12:39:00          969 2019-09-25 21:30:00 2019-11-08 12:39:00 2019-09-27 10:43:00   5.89          969 2019-09-27 10:43:00
20    51 2019-09-12 20:15:00 2019-09-22 18:40:00         1046 2019-09-12 20:15:00 2019-09-22 18:40:00 2019-09-17 11:09:00   3.44         1046 2019-09-17 11:09:00
21    63 2019-09-16 09:40:00 2019-09-30 13:40:00         1202 2019-09-16 09:40:00 2019-09-30 13:40:00 2019-09-18 10:25:00   3.9          1202 2019-09-18 10:25:00
22    55 2019-09-13 20:00:00 2019-10-18 14:00:00         1211 2019-09-13 20:00:00 2019-10-18 14:00:00 2019-09-17 11:10:00   4.07         1211 2019-09-17 11:10:00
23    83 2019-09-24 16:41:00 2019-11-08 12:00:00         1238 2019-09-24 16:41:00 2019-11-08 12:00:00 2019-09-26 10:28:00   3.23         1238 2019-09-26 10:28:00
24    16 2019-08-27 23:10:00 2019-10-03 14:55:00         1304 2019-08-27 23:10:00 2019-10-03 14:55:00 2019-09-04 10:25:00   3.49         1304 2019-09-04 10:25:00
25    27 2019-08-31 17:30:00 2019-09-12 09:00:00         1399 2019-08-31 17:30:00 2019-09-12 09:00:00 2019-09-03 10:29:00   5.38         1399 2019-09-03 10:29:00
26     3 2019-08-22 20:00:00 2019-10-08 12:00:00         1408 2019-08-22 20:00:00 2019-10-08 12:00:00 2019-09-10 11:14:00   3.46         1408 2019-09-10 11:14:00
27    73 2019-09-19 17:08:00 2019-10-09 14:00:00         1413 2019-09-19 17:08:00 2019-10-09 14:00:00 2019-09-23 11:25:00   2.83         1413 2019-09-23 11:25:00
28    39 2019-09-04 21:51:00 2019-11-18 14:06:00         1414 2019-09-04 21:51:00 2019-11-18 14:06:00 2019-09-12 10:17:00   3.63         1414 2019-09-12 10:17:00
29    29 2019-09-02 12:59:00 2019-10-30 10:10:00         1418 2019-09-02 12:59:00 2019-10-30 10:10:00 2019-09-04 10:26:00   2.86         1418 2019-09-04 10:26:00
30    24 2019-08-30 15:00:00 2019-10-14 13:25:00         1435 2019-08-30 15:00:00 2019-10-14 13:25:00 2019-09-02 10:26:00   5.2          1435 2019-09-02 10:26:00
31    22 2019-08-30 10:40:00 2019-11-08 13:00:00         1495 2019-08-30 10:40:00 2019-11-08 13:00:00 2019-09-03 10:29:00   3.65         1495 2019-09-03 10:29:00
32    94 2019-09-29 19:00:00 2019-10-03 13:00:00         1535 2019-09-29 19:00:00 2019-10-03 13:00:00 2019-10-01 10:37:00   6.56         1535 2019-10-01 10:37:00
33    26 2019-08-30 17:20:00 2019-09-20 12:00:00         1555 2019-08-30 17:20:00 2019-09-20 12:00:00 2019-09-02 10:26:00   7.27         1555 2019-09-02 10:26:00
34    99 2019-10-01 12:45:00 2019-10-29 14:20:00         1555 2019-10-01 12:45:00 2019-10-29 14:20:00 2019-10-02 11:02:00   3.46         1555 2019-10-02 11:02:00
35    38 2019-09-04 14:00:00 2019-12-17 13:20:00         1561 2019-09-04 14:00:00 2019-12-17 13:20:00 2019-09-20 10:30:00   2.26         1561 2019-09-20 10:30:00
36     1 2019-08-22 15:06:00 2019-10-09 12:00:00         1565 2019-08-22 15:06:00 2019-10-09 12:00:00 2019-10-09 11:18:00   3.11         1565 2019-10-09 11:18:00
37     4 2019-08-23 14:00:00 2019-11-22 13:40:00         1566 2019-08-23 14:00:00 2019-11-22 13:40:00 2019-09-17 10:56:00   3.15         1566 2019-09-17 10:56:00
38     5 2019-08-23 15:30:00 2019-10-14 16:20:00         1567 2019-08-23 15:30:00 2019-10-14 16:20:00 2019-09-02 10:26:00   4.39         1567 2019-09-02 10:26:00
39     7 2019-08-26 14:15:00 2019-09-24 13:50:00         1568 2019-08-26 14:15:00 2019-09-24 13:50:00 2019-09-23 11:28:00   2.07         1568 2019-09-23 11:28:00
40     9 2019-08-26 17:51:00 2019-09-19 14:00:00         1569 2019-08-26 17:51:00 2019-09-19 14:00:00 2019-09-19 10:26:00   3.2          1569 2019-09-19 10:26:00
41    10 2019-08-26 19:30:00 2020-01-20 16:10:00         1570 2019-08-26 19:30:00 2020-01-20 16:10:00 2019-09-24 10:33:00   2.56         1570 2019-09-24 10:33:00
42    11 2019-08-26 20:45:00 2019-09-17 11:00:00         1571 2019-08-26 20:45:00 2019-09-17 11:00:00 2019-09-16 10:16:00   6.01         1571 2019-09-16 10:16:00
43    13 2019-08-27 14:25:00 2019-09-24 11:10:00         1572 2019-08-27 14:25:00 2019-09-24 11:10:00 2019-09-23 11:27:00   4.93         1572 2019-09-23 11:27:00
44    17 2019-08-28 10:00:00 2019-09-18 14:20:00         1575 2019-08-28 10:00:00 2019-09-18 14:20:00 2019-09-02 10:26:00   3.18         1575 2019-09-02 10:26:00
45    18 2019-08-28 15:41:00 2019-10-02 11:35:00         1576 2019-08-28 15:41:00 2019-10-02 11:35:00 2019-10-01 10:37:00   2.5          1576 2019-10-01 10:37:00
46    19 2019-08-28 21:00:00 2019-10-11 14:10:00         1577 2019-08-28 21:00:00 2019-10-11 14:10:00 2019-10-09 11:18:00   3.22         1577 2019-10-09 11:18:00
47    20 2019-08-29 12:23:00 2019-09-24 12:20:00         1578 2019-08-29 12:23:00 2019-09-24 12:20:00 2019-09-24 11:22:00   3.18         1578 2019-09-24 11:22:00
48    25 2019-08-30 16:00:00 2019-09-27 15:25:00         1579 2019-08-30 16:00:00 2019-09-27 15:25:00 2019-09-03 10:19:00   3.13         1579 2019-09-03 10:19:00
49    28 2019-09-02 03:25:00 2019-09-09 14:45:00         1580 2019-09-02 03:25:00 2019-09-09 14:45:00 2019-09-03 10:29:00   9.04         1580 2019-09-03 10:29:00
50    31 2019-09-02 23:58:00 2019-11-22 13:58:00         1581 2019-09-02 23:58:00 2019-11-22 13:58:00 2019-09-03 10:19:00   3.69         1581 2019-09-03 10:19:00
51    33 2019-09-03 17:00:00 2019-10-18 13:30:00         1582 2019-09-03 17:00:00 2019-10-18 13:30:00 2019-09-06 10:05:00   3.18         1582 2019-09-06 10:05:00
52    34 2019-09-04 12:20:00 2019-11-20 12:11:00         1583 2019-09-04 12:20:00 2019-11-20 12:11:00 2019-09-06 10:05:00   3.44         1583 2019-09-06 10:05:00
53    35 2019-09-04 13:30:00 2019-10-18 12:25:00         1584 2019-09-04 13:30:00 2019-10-18 12:25:00 2019-09-06 10:05:00   3.02         1584 2019-09-06 10:05:00
54    36 2019-09-04 14:00:00 2019-10-21 11:35:00         1585 2019-09-04 14:00:00 2019-10-21 11:35:00 2019-09-06 10:15:00   4.14         1585 2019-09-06 10:15:00
55    37 2019-09-05 12:12:00 2019-10-01 13:15:00         1586 2019-09-05 12:12:00 2019-10-01 13:15:00 2019-09-09 11:28:00   4.98         1586 2019-09-09 11:28:00
56    40 2019-09-04 23:50:00 2019-10-01 13:00:00         1587 2019-09-04 23:50:00 2019-10-01 13:00:00 2019-09-30 10:56:00   3.17         1587 2019-09-30 10:56:00
57    41 2019-09-05 22:00:00 2019-09-27 11:14:00         1588 2019-09-05 22:00:00 2019-09-27 11:14:00 2019-09-09 11:28:00   3.66         1588 2019-09-09 11:28:00
58    42 2019-09-06 19:05:00 2019-10-21 13:40:00         1589 2019-09-06 19:05:00 2019-10-21 13:40:00 2019-09-10 11:14:00   2.68         1589 2019-09-10 11:14:00
59    43 2019-09-07 04:20:00 2019-10-08 14:00:00         1590 2019-09-07 04:20:00 2019-10-08 14:00:00 2019-09-10 11:14:00   3.02         1590 2019-09-10 11:14:00
60    44 2019-09-09 01:18:00 2019-09-19 12:20:00         1591 2019-09-09 01:18:00 2019-09-19 12:20:00 2019-09-10 11:01:00   1.69         1591 2019-09-10 11:01:00
61    48 2019-09-11 15:00:00 2019-10-03 14:50:00         1593 2019-09-11 15:00:00 2019-10-03 14:50:00 2019-09-13 10:42:00   5.01         1593 2019-09-13 10:42:00
62    49 2019-09-12 01:25:00 2019-12-16 14:30:00         1594 2019-09-12 01:25:00 2019-12-16 14:30:00 2019-09-13 10:42:00   1.87         1594 2019-09-13 10:42:00
63    50 2019-09-12 14:30:00 2019-10-07 12:30:00         1595 2019-09-12 14:30:00 2019-10-07 12:30:00 2019-09-16 10:16:00   5            1595 2019-09-16 10:16:00
64    52 2019-09-13 02:08:00 2019-10-18 13:30:00         1596 2019-09-13 02:08:00 2019-10-18 13:30:00 2019-09-16 10:21:00   4.07         1596 2019-09-16 10:21:00
65    53 2019-09-13 12:00:00 2019-10-23 11:30:00         1597 2019-09-13 12:00:00 2019-10-23 11:30:00 2019-09-17 11:14:00   5.7          1597 2019-09-17 11:14:00
66    56 2019-09-13 23:55:00 2019-11-05 12:31:00         1598 2019-09-13 23:55:00 2019-11-05 12:31:00 2019-09-16 10:21:00   3.52         1598 2019-09-16 10:21:00
67    57 2019-09-14 03:30:00 2019-10-24 13:30:00         1599 2019-09-14 03:30:00 2019-10-24 13:30:00 2019-09-16 10:16:00   3.36         1599 2019-09-16 10:16:00
68    58 2019-09-14 10:14:00 2019-11-06 12:20:00         1600 2019-09-14 10:14:00 2019-11-06 12:20:00 2019-09-17 10:56:00   4.68         1600 2019-09-17 10:56:00
69    61 2019-09-14 22:00:00 2019-10-24 13:30:00         1601 2019-09-14 22:00:00 2019-10-24 13:30:00 2019-09-17 10:56:00   3.51         1601 2019-09-17 10:56:00
70    64 2019-09-16 14:30:00 2019-11-12 13:56:00         1602 2019-09-16 14:30:00 2019-11-12 13:56:00 2019-09-18 10:25:00   3.23         1602 2019-09-18 10:25:00
71    65 2019-09-16 18:39:00 2019-10-21 14:55:00         1603 2019-09-16 18:39:00 2019-10-21 14:55:00 2019-09-19 10:36:00   1.68         1603 2019-09-19 10:36:00
72    66 2019-09-17 11:05:00 2019-10-09 14:19:00         1604 2019-09-17 11:05:00 2019-10-09 14:19:00 2019-09-19 10:26:00   3.09         1604 2019-09-19 10:26:00
73    68 2019-09-18 15:30:00 2019-10-16 14:15:00         1605 2019-09-18 15:30:00 2019-10-16 14:15:00 2019-09-24 11:38:00   3.83         1605 2019-09-24 11:38:00
74    69 2019-09-18 16:50:00 2019-12-06 13:34:00         1606 2019-09-18 16:50:00 2019-12-06 13:34:00 2019-09-20 10:30:00   3.26         1606 2019-09-20 10:30:00
75    70 2019-09-19 10:40:00 2019-12-13 12:07:00         1607 2019-09-19 10:40:00 2019-12-13 12:07:00 2019-09-23 11:50:00   3.34         1607 2019-09-23 11:50:00
76    71 2019-09-19 11:55:00 2019-12-23 12:30:00         1608 2019-09-19 11:55:00 2019-12-23 12:30:00 2019-09-23 11:54:00   3.08         1608 2019-09-23 11:54:00
77    72 2019-09-19 15:30:00 2019-09-30 10:25:00         1609 2019-09-19 15:30:00 2019-09-30 10:25:00 2019-09-23 11:27:00   5.18         1609 2019-09-23 11:27:00
78    74 2019-09-19 21:58:00 2019-10-16 13:22:00         1610 2019-09-19 21:58:00 2019-10-16 13:22:00 2019-09-23 11:26:00   1.61         1610 2019-09-23 11:26:00
79    76 2019-09-21 17:30:00 2019-10-18 12:30:00         1611 2019-09-21 17:30:00 2019-10-18 12:30:00 2019-09-24 11:07:00   3.71         1611 2019-09-24 11:07:00
80    77 2019-09-21 19:00:00 2019-09-26 12:10:00         1612 2019-09-21 19:00:00 2019-09-26 12:10:00 2019-09-23 11:25:00   6.08         1612 2019-09-23 11:25:00
81    78 2019-09-22 08:30:00 2019-12-05 13:30:00         1613 2019-09-22 08:30:00 2019-12-05 13:30:00 2019-09-24 11:08:00   1.5          1613 2019-09-24 11:08:00
82    79 2019-09-22 13:00:00 2019-12-05 18:39:00         1614 2019-09-22 13:00:00 2019-12-05 18:39:00 2019-09-24 11:37:00   1.76         1614 2019-09-24 11:37:00
83    80 2019-09-23 16:10:00 2019-10-14 14:50:00         1615 2019-09-23 16:10:00 2019-10-14 14:50:00 2019-09-25 10:47:00   2.23         1615 2019-09-25 10:47:00
84    81 2019-09-23 19:10:00 2019-11-05 15:11:00         1616 2019-09-23 19:10:00 2019-11-05 15:11:00 2019-09-27 10:43:00   2.21         1616 2019-09-27 10:43:00
85    84 2019-09-24 17:45:00 2019-10-29 14:01:00         1617 2019-09-24 17:45:00 2019-10-29 14:01:00 2019-09-26 10:27:00   4.57         1617 2019-09-26 10:27:00
86    85 2019-09-25 12:50:00 2019-10-25 12:30:00         1618 2019-09-25 12:50:00 2019-10-25 12:30:00 2019-09-26 10:27:00   2.86         1618 2019-09-26 10:27:00
87    86 2019-09-25 14:50:00 2019-12-23 17:00:00         1619 2019-09-25 14:50:00 2019-12-23 17:00:00 2019-09-27 10:43:00   2.08         1619 2019-09-27 10:43:00
88    89 2019-09-26 10:32:00 2019-10-21 12:20:00         1620 2019-09-26 10:32:00 2019-10-21 12:20:00 2019-09-27 10:43:00   2.32         1620 2019-09-27 10:43:00
89    90 2019-09-27 10:44:00 2019-12-27 13:37:00         1621 2019-09-27 10:44:00 2019-12-27 13:37:00 2019-10-03 10:50:00   4.02         1621 2019-10-03 10:50:00
90    91 2019-09-27 18:00:00 2019-10-17 15:10:00         1622 2019-09-27 18:00:00 2019-10-17 15:10:00 2019-10-01 10:37:00   3.49         1622 2019-10-01 10:37:00
91    92 2019-09-28 05:49:00 2019-10-07 13:30:00         1623 2019-09-28 05:49:00 2019-10-07 13:30:00 2019-10-04 11:01:00   5.96         1623 2019-10-04 11:01:00
92    95 2019-09-29 21:50:00 2019-10-09 14:00:00         1624 2019-09-29 21:50:00 2019-10-09 14:00:00 2019-10-01 10:37:00   1.66         1624 2019-10-01 10:37:00
93    96 2019-09-30 11:50:00 2019-10-07 14:15:00         1625 2019-09-30 11:50:00 2019-10-07 14:15:00 2019-10-02 11:02:00   3.6          1625 2019-10-02 11:02:00
94    97 2019-09-30 13:20:00 2019-10-18 13:30:00         1626 2019-09-30 13:20:00 2019-10-18 13:30:00 2019-10-02 11:02:00   2.19         1626 2019-10-02 11:02:00
95    98 2019-09-30 13:50:00 2019-10-30 12:40:00         1627 2019-09-30 13:50:00 2019-10-30 12:40:00 2019-10-02 11:02:00   4.69         1627 2019-10-02 11:02:00
96   100 2019-10-01 13:15:00 2019-10-22 14:00:00         1628 2019-10-01 13:15:00 2019-10-22 14:00:00 2019-10-04 10:58:00   4.08         1628 2019-10-04 10:58:00

Note: Reviewing Patient_ID of 510, the test result pulled is 9/27/19. This appears to be the first/earliest test result during the hospitalization from 9/25/19-11/21/19.

     Test_date Test_time Value Patient_ID           Test.date
19  2019-10-18  11:00:00  1.87        510 2019-10-18 11:00:00
50  2019-10-14  10:45:00  1.18        510 2019-10-14 10:45:00
144 2019-09-27  10:43:00  1.35        510 2019-09-27 10:43:00
228 2019-09-13  22:27:00  5.52        510 2019-09-13 22:27:00
262 2019-09-09  10:20:00  2.77        510 2019-09-09 10:20:00

Solution 2:

Here is one approach using dplyr and lubridate

  1. Make a minimal data set. I had to change some of your values to make sure that multiple test occurred during and admission
  2. Join the two data sets by Patient.ID
  3. Compute the time from admission to test (time_to_test) and if the test was during and admission (test_during_admission)
  4. Filter out only the one where test_during_admission was true
  5. Find the minimum time_to_test per patient id.

You will want to think through what might happen if a patient has multiple admissions. You may need to do additional filtering.


library(dplyr)
library(lubridate)


df_joined<-df1 %>%
  mutate(Admission.date=mdy_hm(Admission.date),
         Discharge.date=mdy_hm(Discharge.date)
  ) %>%
  left_join(df2) %>%
  mutate(Test.date=mdy_hm(Test.date)) %>%
  mutate(test_during_admission=Test.date %within% (Admission.date%--%Discharge.date)) %>%
  mutate(time_to_test=((Admission.date%--%Test.date) %>% as.duration)/dminutes(1)) %>%
  filter(test_during_admission) %>%
  group_by(Patient.ID) %>%
    slice_min(time_to_test) %>%
  ungroup() %>%
  select(-test_during_admission,-time_to_test)


df_joined
#> # A tibble: 1 × 5
#>   Admission.date      Discharge.date      Patient.ID Test.date           Value
#>   <dttm>              <dttm>                   <int> <dttm>              <dbl>
#> 1 2017-01-02 11:00:00 2017-03-23 11:10:00         55 2017-01-03 09:57:00  5.76


#DATA

df1<-tibble::tribble(
  ~Admission.date, ~Discharge.date, ~Patient.ID,
   "1/2/17 11:00", "3/23/17 11:10",         55L,
   "1/2/17 15:00",  "2/3/17 14:27",         97L,
   "1/2/17 16:00", "3/15/17 13:30",         72L,
   "1/3/17 11:40", "3/10/17 14:00",         86L,
   "1/3/17 13:01",  "3/8/17 13:15",         50L,
   "1/3/17 18:45", "2/14/17 14:10",         53L,
   "1/3/17 20:18",  "2/3/17 14:00",         48L,
   "1/3/17 20:18",  "1/27/17 9:50",         65L,
   "1/4/17 12:30", "1/24/17 16:30",         37L,
   "1/4/17 18:15", "2/16/17 11:38",         49L
  )


df2<-tibble::tribble(
  ~Patient.ID,       ~Test.date, ~Value,
          55L,  "1/3/17 9:57",   5.76,
          55L,  "1/3/17 19:57",   8.44,
          86L,  "12/31/21 9:57",   6.29,
          88L,  "12/31/21 9:57",   7.29,
          82L,  "12/31/21 9:57",   7.12,
          77L,  "12/31/21 9:57",   7.28,
          83L,  "12/31/21 9:57",    5.7,
          51L,  "12/31/21 9:57",   4.12,
          84L,  "12/31/21 9:57",   6.76,
          94L, "12/30/21 11:20",   9.32
  )


Created on 2022-01-16 by the reprex package (v2.0.1)