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

Viewed 63

I have two data frames:

  1. The first contains information on hospitalization and has 3706 observations:
    1   2019-08-22 15:06:00 2019-10-09 12:00:00       1565
    2   2019-08-22 16:15:00 2019-09-12 12:33:00          3
    3   2019-08-22 20:00:00 2019-10-08 12:00:00       1408
    4   2019-08-23 14:00:00 2019-11-22 13:40:00       1566
    5   2019-08-23 15:30:00 2019-10-14 16:20:00       1567
    6   2019-08-24 12:30:00 2019-09-19 12:11:00        268
    7   2019-08-26 14:15:00 2019-09-24 13:50:00       1568
    8   2019-08-26 15:50:00 2019-10-29 13:47:00        161
    9   2019-08-26 17:51:00 2019-09-19 14:00:00       1569
    10  2019-08-26 19:30:00 2020-01-20 16:10:00       1570
    11  2019-08-26 20:45:00 2019-09-17 11:00:00       1571
    12  2019-08-26 21:10:00 2020-01-10 14:30:00        702
    13  2019-08-27 14:25:00 2019-09-24 11:10:00       1572
    14  2019-08-27 16:46:00 2019-08-30 15:18:00       1573
    15  2019-08-27 19:45:00 2019-09-02 13:45:00       1574
    16  2019-08-27 23:10:00 2019-10-03 14:55:00       1304
    17  2019-08-28 10:00:00 2019-09-18 14:20:00       1575
    18  2019-08-28 15:41:00 2019-10-02 11:35:00       1576
    19  2019-08-28 21:00:00 2019-10-11 14:10:00       1577
    20  2019-08-29 12:23:00 2019-09-24 12:20:00       1578
    21  2019-08-29 19:30:00 2019-09-25 12:30:00        599
    22  2019-08-30 10:40:00 2019-11-08 13:00:00       1495
    23  2019-08-30 12:40:00 2019-09-23 12:20:00         33
    24  2019-08-30 15:00:00 2019-10-14 13:25:00       1435
    25  2019-08-30 16:00:00 2019-09-27 15:25:00       1579
    26  2019-08-30 17:20:00 2019-09-20 12:00:00       1555
    27  2019-08-31 17:30:00 2019-09-12 09:00:00       1399
    28  2019-09-02 03:25:00 2019-09-09 14:45:00       1580
    29  2019-09-02 12:59:00 2019-10-30 10:10:00       1418
    30  2019-09-02 18:20:00 2019-09-20 16:10:00        766
    31  2019-09-02 23:58:00 2019-11-22 13:58:00       1581
    32  2019-09-03 11:15:00 2019-10-22 10:00:00        519
    33  2019-09-03 17:00:00 2019-10-18 13:30:00       1582
    34  2019-09-04 12:20:00 2019-11-20 12:11:00       1583
    35  2019-09-04 13:30:00 2019-10-18 12:25:00       1584
    36  2019-09-04 14:00:00 2019-10-21 11:35:00       1585
    37  2019-09-05 12:12:00 2019-10-01 13:15:00       1586
    38  2019-09-04 14:00:00 2019-12-17 13:20:00       1561
    39  2019-09-04 21:51:00 2019-11-18 14:06:00       1414
    40  2019-09-04 23:50:00 2019-10-01 13:00:00       1587
    41  2019-09-05 22:00:00 2019-09-27 11:14:00       1588
    42  2019-09-06 19:05:00 2019-10-21 13:40:00       1589
    43  2019-09-07 04:20:00 2019-10-08 14:00:00       1590
    44  2019-09-09 01:18:00 2019-09-19 12:20:00       1591
    45  2019-09-10 12:00:00 2019-10-16 10:15:00        409
    46  2019-09-10 14:15:00 2019-10-16 14:30:00        279
    47  2019-09-10 16:00:00 2019-09-11 11:40:00       1592
    48  2019-09-11 15:00:00 2019-10-03 14:50:00       1593
    49  2019-09-12 01:25:00 2019-12-16 14:30:00       1594
    50  2019-09-12 14:30:00 2019-10-07 12:30:00       1595
    51  2019-09-12 20:15:00 2019-09-22 18:40:00       1046
    52  2019-09-13 02:08:00 2019-10-18 13:30:00       1596
    53  2019-09-13 12:00:00 2019-10-23 11:30:00       1597
    54  2019-09-13 16:55:00 2019-09-27 08:09:00         94
    55  2019-09-13 20:00:00 2019-10-18 14:00:00       1211
    56  2019-09-13 23:55:00 2019-11-05 12:31:00       1598
    57  2019-09-14 03:30:00 2019-10-24 13:30:00       1599
    58  2019-09-14 10:14:00 2019-11-06 12:20:00       1600
    59  2019-09-14 11:35:00 2019-10-15 13:20:00         44
    60  2019-09-14 16:30:00 2019-09-30 12:24:00        473
    61  2019-09-14 22:00:00 2019-10-24 13:30:00       1601
    62  2019-09-15 11:50:00 2019-11-26 09:48:00        274
    63  2019-09-16 09:40:00 2019-09-30 13:40:00       1202
    64  2019-09-16 14:30:00 2019-11-12 13:56:00       1602
    65  2019-09-16 18:39:00 2019-10-21 14:55:00       1603
    66  2019-09-17 11:05:00 2019-10-09 14:19:00       1604
    67  2019-09-17 23:55:00 2019-12-03 11:50:00        443
    68  2019-09-18 15:30:00 2019-10-16 14:15:00       1605
    69  2019-09-18 16:50:00 2019-12-06 13:34:00       1606
    70  2019-09-19 10:40:00 2019-12-13 12:07:00       1607
    71  2019-09-19 11:55:00 2019-12-23 12:30:00       1608
    72  2019-09-19 15:30:00 2019-09-30 10:25:00       1609
    73  2019-09-19 17:08:00 2019-10-09 14:00:00       1413
    74  2019-09-19 21:58:00 2019-10-16 13:22:00       1610
    75  2019-09-20 09:30:00 2019-11-29 13:45:00       1541
    76  2019-09-21 17:30:00 2019-10-18 12:30:00       1611
    77  2019-09-21 19:00:00 2019-09-26 12:10:00       1612
    78  2019-09-22 08:30:00 2019-12-05 13:30:00       1613
    79  2019-09-22 13:00:00 2019-12-05 18:39:00       1614
    80  2019-09-23 16:10:00 2019-10-14 14:50:00       1615
    81  2019-09-23 19:10:00 2019-11-05 15:11:00       1616
    82  2019-09-24 14:30:00 2019-10-22 13:30:00        522
    83  2019-09-24 16:41:00 2019-11-08 12:00:00       1238
    84  2019-09-24 17:45:00 2019-10-29 14:01:00       1617
    85  2019-09-25 12:50:00 2019-10-25 12:30:00       1618
    86  2019-09-25 14:50:00 2019-12-23 17:00:00       1619
    87  2019-09-25 16:15:00 2019-11-21 14:44:00        510
    88  2019-09-25 21:30:00 2019-11-08 12:39:00        969
    89  2019-09-26 10:32:00 2019-10-21 12:20:00       1620
    90  2019-09-27 10:44:00 2019-12-27 13:37:00       1621
    91  2019-09-27 18:00:00 2019-10-17 15:10:00       1622
    92  2019-09-28 05:49:00 2019-10-07 13:30:00       1623
    93  2019-09-29 16:45:00 2019-10-23 13:30:00         94
    94  2019-09-29 19:00:00 2019-10-03 13:00:00       1535
    95  2019-09-29 21:50:00 2019-10-09 14:00:00       1624
    96  2019-09-30 11:50:00 2019-10-07 14:15:00       1625
    97  2019-09-30 13:20:00 2019-10-18 13:30:00       1626
    98  2019-09-30 13:50:00 2019-10-30 12:40:00       1627
    99  2019-10-01 12:45:00 2019-10-29 14:20:00       1555
    100 2019-10-01 13:15:00 2019-10-22 14:00:00       1628
    101 2019-10-01 19:10:00 2019-10-17 13:40:00        935
  1. The second one contains data from the results of tests carried out during hospitalization an has 7931 observations.
              Test_date Value Patient_ID
1   2019-10-21 11:39:00  2.23       1614
2   2019-10-21 11:39:00  5.25         51
3   2019-10-21 11:05:00  4.63       1644
4   2019-10-21 11:05:00  4.65       1617
5   2019-10-21 11:05:00  3.37       1656
6   2019-10-21 10:37:00  2.06       1594
7   2019-10-21 10:37:00  7.24       1649
8   2019-10-21 10:37:00  2.44       1619
9   2019-10-21 10:37:00  4.27       1621
10  2019-10-21 10:37:00  6.15       1581
11  2019-10-21 10:37:00  3.28        443
12  2019-10-21 10:37:00  2.22       1406
13  2019-10-21 10:37:00  3.90       1551
14  2019-10-18 11:00:00  4.83       1585
15  2019-10-18 11:00:00  2.43       1626
16  2019-10-18 11:00:00  2.13       1620
17  2019-10-18 11:00:00  4.48       1628
18  2019-10-18 11:00:00  4.63       1637
19  2019-10-18 11:00:00  1.87        510
20  2019-10-17 11:12:00  1.70       1389
21  2019-10-17 11:12:00  3.24       1596
22  2019-10-17 11:12:00  5.00       1647
23  2019-10-17 11:11:00  2.69       1418
24  2019-10-17 11:11:00  3.32       1584
25  2019-10-17 11:11:00  2.80       1211
26  2019-10-16 10:15:00  5.83       1646
27  2019-10-16 10:15:00  2.22       1472
28  2019-10-16 10:15:00  3.29       1495
29  2019-10-16 10:15:00  4.00       1605
30  2019-10-16 10:15:00  4.99         12
31  2019-10-16 10:15:00  3.29       1645
32  2019-10-16 10:15:00  2.54       1582
33  2019-10-16 10:15:00  4.31       1618
34  2019-10-15 11:11:00  3.26       1610
35  2019-10-15 11:11:00  3.64       1598
36  2019-10-15 10:32:00  2.45        409
37  2019-10-15 10:32:00  2.45       1643
38  2019-10-15 10:32:00  2.06       1640
39  2019-10-15 10:32:00  4.96       1644
40  2019-10-15 10:31:00  4.87        279
41  2019-10-14 10:54:00  2.30       1614
42  2019-10-14 10:54:00  7.86       1638
43  2019-10-14 10:46:00  2.35       1641
44  2019-10-14 10:46:00  5.16       1644
45  2019-10-14 10:46:00  4.08       1631
46  2019-10-14 10:46:00  1.97       1615
47  2019-10-14 10:45:00  3.85       1621
48  2019-10-14 10:45:00  2.75         44
49  2019-10-14 10:45:00  1.92       1642
50  2019-10-14 10:45:00  1.18        510
51  2019-10-14 10:30:00  2.31       1619
52  2019-10-11 11:29:00  2.07       1642
53  2019-10-11 11:29:00  3.15       1639
54  2019-10-11 11:29:00  3.75       1611
55  2019-10-11 11:29:00  1.03       1374
56  2019-10-11 11:29:00  4.36       1551
57  2019-10-11 11:29:00  4.77       1588
58  2019-10-11 11:28:00  1.64        151
59  2019-10-11 11:28:00  5.57       1638
60  2019-10-11 11:28:00  4.18       1435
61  2019-10-11 11:28:00  2.98       1538
62  2019-10-11 11:28:00  3.60       1636
63  2019-10-11 11:28:00  1.48         94
64  2019-10-10 10:39:00  3.44       1636
65  2019-10-10 10:39:00  2.50       1570
66  2019-10-10 10:24:00  3.73       1567
67  2019-10-09 11:19:00  3.26        985
68  2019-10-09 11:19:00  3.55        161
69  2019-10-09 11:18:00  4.18       1604
70  2019-10-09 11:18:00  4.30         51
71  2019-10-09 11:18:00  3.87        279
72  2019-10-09 11:18:00  3.22       1577
73  2019-10-09 11:18:00  3.11       1565
74  2019-10-09 11:18:00  2.58       1614
75  2019-10-09 11:18:00  1.96       1613
76  2019-10-08 11:33:00  6.11       1631
77  2019-10-08 11:32:00  4.25       1634
78  2019-10-08 11:32:00  2.20       1635
79  2019-10-08 11:04:00  3.53       1632
80  2019-10-08 11:04:00  2.06       1633
81  2019-10-08 11:04:00  2.61       1614
82  2019-10-08 11:04:00  6.95       1552
83  2019-10-07 11:04:00  2.52       1608
84  2019-10-07 11:04:00  2.54       1619
85  2019-10-07 11:04:00  3.17       1589
86  2019-10-07 11:04:00  2.80       1582
87  2019-10-07 11:04:00  3.83       1607
88  2019-10-07 11:03:00  4.49         12
89  2019-10-07 11:03:00  4.64       1629
90  2019-10-07 11:03:00  6.61       1597
91  2019-10-07 11:03:00  3.87       1630
92  2019-10-07 11:03:00  4.21       1618
93  2019-10-07 11:03:00  4.58       1408
94  2019-10-07 11:03:00  4.89       1595
95  2019-10-07 11:03:00  3.52        954
96  2019-10-04 11:02:00  3.92        935
97  2019-10-04 11:02:00  2.41       1556
98  2019-10-04 11:02:00  3.44       1598
99  2019-10-04 11:02:00  1.49       1561
100 2019-10-04 11:01:00  8.38       1597
101 2019-10-04 11:01:00  4.06       1544
102 2019-10-04 11:01:00  3.52        216
103 2019-10-04 11:01:00  5.96       1623
104 2019-10-04 11:01:00  5.23       1606
105 2019-10-04 10:58:00  4.08       1628
106 2019-10-03 10:51:00  1.84       1603
107 2019-10-03 10:50:00  4.02       1621
108 2019-10-03 10:50:00  3.75       1304
109 2019-10-03 10:39:00  2.67       1495
110 2019-10-03 10:39:00  4.59        519
111 2019-10-03 10:39:00  3.96       1527
112 2019-10-02 11:02:00  2.20        528
113 2019-10-02 11:02:00  2.64       1538
114 2019-10-02 11:02:00  3.60       1625
115 2019-10-02 11:02:00  4.69       1627
116 2019-10-02 11:02:00  2.33       1619
117 2019-10-02 11:02:00  3.79         10
118 2019-10-02 11:02:00  3.46       1555
119 2019-10-02 11:02:00  2.19       1626
120 2019-10-01 10:37:00  1.66       1624
121 2019-10-01 10:37:00  3.93       1341
122 2019-10-01 10:37:00  3.49       1622
123 2019-10-01 10:37:00  2.41       1614
124 2019-10-01 10:37:00  6.56       1535
125 2019-10-01 10:37:00  2.50       1576
126 2019-10-01 10:36:00  4.00       1553
127 2019-09-30 10:56:00  8.94       1091
128 2019-09-30 10:56:00  3.94       1599
129 2019-09-30 10:56:00  3.26       1618
130 2019-09-30 10:56:00  6.08       1552
131 2019-09-30 10:56:00  3.17       1587
132 2019-09-30 10:56:00  7.17       1380
133 2019-09-30 10:56:00  4.35       1551
134 2019-09-30 10:55:00  3.20       1546
135 2019-09-30 10:55:00  4.06         44
136 2019-09-30 10:18:00  2.37       1619
137 2019-09-27 16:05:00  2.50       1619
138 2019-09-27 10:43:00  2.32       1620
139 2019-09-27 10:43:00  2.08       1619
140 2019-09-27 10:43:00  5.89        969
141 2019-09-27 10:43:00  3.03         10
142 2019-09-27 10:43:00  3.12       1579
143 2019-09-27 10:43:00  2.21       1616
144 2019-09-27 10:43:00  1.35        510
145 2019-09-27 10:43:00  2.95       1531
146 2019-09-26 10:32:00  5.95       1552
147 2019-09-26 10:31:00  4.32       1544
148 2019-09-26 10:28:00  4.07        279
149 2019-09-26 10:28:00  3.23       1238
150 2019-09-26 10:28:00  1.80        702
151 2019-09-26 10:28:00  2.72       1615
152 2019-09-26 10:27:00  2.86       1618
153 2019-09-26 10:27:00  4.57       1617
154 2019-09-25 10:47:00  1.31         94
155 2019-09-25 10:47:00  3.12       1582
156 2019-09-25 10:47:00  2.23       1615
157 2019-09-25 10:47:00  5.15        599
158 2019-09-24 11:38:00  3.83       1605
159 2019-09-24 11:37:00  3.92       1586
160 2019-09-24 11:37:00  1.76       1614
161 2019-09-24 11:22:00  3.18       1578
162 2019-09-24 11:08:00  4.54       1562
163 2019-09-24 11:08:00  1.50       1613
164 2019-09-24 11:08:00  3.58       1593
165 2019-09-24 11:07:00  3.71       1611
166 2019-09-24 10:33:00  2.56       1570
167 2019-09-23 11:54:00  3.08       1608
168 2019-09-23 11:50:00  3.34       1607
169 2019-09-23 11:50:00  7.53       1552
170 2019-09-23 11:29:00  5.88       1553
171 2019-09-23 11:28:00  2.07       1568
172 2019-09-23 11:28:00  3.37        216
173 2019-09-23 11:28:00  3.54       1546
174 2019-09-23 11:27:00  4.93       1572
175 2019-09-23 11:27:00  5.18       1609
176 2019-09-23 11:26:00  6.45       1597
177 2019-09-23 11:26:00  3.05       1588
178 2019-09-23 11:26:00  1.61       1610
179 2019-09-23 11:26:00  4.65       1536
180 2019-09-23 11:26:00  3.11        702
181 2019-09-23 11:25:00  2.83       1413
182 2019-09-23 11:25:00  6.08       1612
183 2019-09-20 10:58:00  3.41       1551
184 2019-09-20 10:58:00  5.82       1211
185 2019-09-20 10:57:00  6.06       1304
186 2019-09-20 10:30:00  3.26       1606
187 2019-09-20 10:30:00  2.26       1561
188 2019-09-19 10:36:00  3.95       1544
189 2019-09-19 10:36:00  8.17       1562
190 2019-09-19 10:36:00  1.95       1591
191 2019-09-19 10:36:00  1.68       1603
192 2019-09-19 10:26:00  3.09       1604
193 2019-09-19 10:26:00  2.00         15
194 2019-09-19 10:26:00  3.10        410
195 2019-09-19 10:26:00  3.86       1091
196 2019-09-19 10:26:00  6.24       1552
197 2019-09-19 10:26:00  3.92       1546
198 2019-09-19 10:26:00  3.20       1569
199 2019-09-18 10:52:00  4.88       1554
200 2019-09-18 10:25:00  3.22       1418
201 2019-09-18 10:25:00  2.01        473
202 2019-09-18 10:25:00  3.23       1602
203 2019-09-18 10:25:00  3.90       1202
204 2019-09-17 11:14:00  5.70       1597
205 2019-09-17 11:10:00  4.07       1211
206 2019-09-17 11:10:00  2.38       1575
207 2019-09-17 11:09:00  7.88       1552
208 2019-09-17 11:09:00  3.54        274
209 2019-09-17 11:09:00  3.44       1046
210 2019-09-17 11:08:00  3.68       1567
211 2019-09-17 10:56:00  3.15       1566
212 2019-09-17 10:56:00  4.68       1600
213 2019-09-17 10:56:00  3.51       1601
214 2019-09-17 10:55:00  1.55         94
215 2019-09-17 10:55:00  1.92         44
216 2019-09-16 10:21:00  2.61       1519
217 2019-09-16 10:21:00  4.07       1596
218 2019-09-16 10:21:00  5.16        268
219 2019-09-16 10:21:00  3.52       1598
220 2019-09-16 10:16:00  1.50        702
221 2019-09-16 10:16:00  8.65       1552
222 2019-09-16 10:16:00  6.01       1571
223 2019-09-16 10:16:00  3.97       1527
224 2019-09-16 10:16:00  5.37       1551
225 2019-09-16 10:16:00  3.36       1599
226 2019-09-16 10:16:00  1.90        409
227 2019-09-16 10:16:00  5.00       1595
228 2019-09-13 22:27:00  5.52        510
229 2019-09-13 11:15:00  2.85       1575
230 2019-09-13 10:43:00  3.48        268
231 2019-09-13 10:43:00  2.68       1558
232 2019-09-13 10:43:00  4.46        519
233 2019-09-13 10:43:00  4.67       1478
234 2019-09-13 10:43:00  8.55         10
235 2019-09-13 10:42:00  1.87       1594
236 2019-09-13 10:42:00  5.01       1593
237 2019-09-12 10:37:00  3.68       1533
238 2019-09-12 10:17:00  3.51        279
239 2019-09-12 10:17:00  3.63       1414
240 2019-09-12 10:17:00  2.46       1540
241 2019-09-11 10:45:00  7.23        664
242 2019-09-11 10:34:00  1.76       1543
243 2019-09-11 10:34:00  5.98       1553
244 2019-09-11 10:33:00  2.44       1551
245 2019-09-11 10:33:00  3.27        232
246 2019-09-10 11:41:00  7.56       1552
247 2019-09-10 11:15:00  4.44         30
248 2019-09-10 11:14:00  2.67       1538
249 2019-09-10 11:14:00  2.68       1589
250 2019-09-10 11:14:00  3.46       1408
251 2019-09-10 11:14:00  3.02       1590
252 2019-09-10 11:14:00  3.85       1567
253 2019-09-10 11:14:00  3.56       1501
254 2019-09-10 11:01:00  4.70       1549
255 2019-09-10 11:01:00  1.69       1591
256 2019-09-10 11:01:00  2.79       1361
257 2019-09-10 11:01:00  4.26       1575
258 2019-09-09 11:28:00  4.98       1586
259 2019-09-09 11:28:00  3.97       1547
260 2019-09-09 11:28:00  3.66       1588
261 2019-09-09 10:28:00  2.60       1564
262 2019-09-09 10:20:00  2.77        510
263 2019-09-06 10:15:00  4.14       1585
264 2019-09-06 10:15:00  3.02       1581
265 2019-09-06 10:15:00  4.50       1544
266 2019-09-06 10:13:00  2.04          3
267 2019-09-06 10:05:00  9.40       1537
268 2019-09-06 10:05:00  3.02       1584
269 2019-09-06 10:05:00  3.44       1583
270 2019-09-06 10:05:00  3.18       1582
271 2019-09-06 10:02:00  4.46         76
272 2019-09-06 10:00:00  4.68       1534
273 2019-09-04 10:26:00  4.81       1580
274 2019-09-04 10:26:00  2.86       1418
275 2019-09-04 10:26:00  3.61       1575
276 2019-09-04 10:26:00  4.22       1502
277 2019-09-04 10:26:00  4.15        766
278 2019-09-04 10:25:00  6.05        599
279 2019-09-04 10:25:00  3.49       1304
280 2019-09-03 10:49:00  1.85       1543
281 2019-09-03 10:29:00  9.04       1580
282 2019-09-03 10:29:00  3.65       1495
283 2019-09-03 10:29:00  1.89       1497
284 2019-09-03 10:29:00  1.43       1539
285 2019-09-03 10:29:00  3.54         33
286 2019-09-03 10:29:00  2.69       1540
287 2019-09-03 10:29:00  5.38       1399
288 2019-09-03 10:29:00  3.78       1435
289 2019-09-03 10:19:00  3.69       1581
290 2019-09-03 10:19:00  4.25       1541
291 2019-09-03 10:19:00  3.13       1579
292 2019-09-02 11:50:00  5.58        216
293 2019-09-02 10:26:00  5.20       1435
294 2019-09-02 10:26:00  7.68       1361
295 2019-09-02 10:26:00  3.25       1551
296 2019-09-02 10:26:00  2.39       1464
297 2019-09-02 10:26:00  3.18       1575
298 2019-09-02 10:26:00  4.39       1567
299 2019-09-02 10:26:00  7.27       1555
300 2019-09-02 10:26:00  8.91       1380
301 2019-08-30 11:15:00  3.34       1538

I would like to create two new columns in the first data frame. The first one should contain the value of the first test performed within the time frame (test date between the admission date and the discharge date) of the given hospitalization with a matching patient ID. The second should contain the date of the test, the value of which is included in the first column.

The number of tests performed during one hospitalization varies and ranges from one to a dozen or so. It also happens that one patient has many hospitalizations listed in the first data frame and many tests within each of them.

So far, I have experimented with converting both frames into lists in which the individual first-order elements correspond to the patient's ID, while the second-order elements correspond to the remaining data contained in the data frame. However, I have no idea how to match and properly filter the test values ​​from the lists resulting from the second frame with data from the list resulting from the first frame.

I would appreciate any tip on how I could solve this problem.

EDIT: Sample data str:

df1:

'data.frame':   101 obs. of  3 variables:
 $ Admission_date: POSIXct, format: "2019-04-17 17:00:00" "2019-04-16 23:55:00" "2019-04-16 18:25:00" "2019-04-16 13:00:00" ...
 $ Discharge_date: POSIXct, format: "2019-06-03 11:10:00" "2019-05-15 15:31:00" "2019-05-07 13:00:00" "2019-04-26 13:00:00" ...
 $ Patient_ID    : int  1571 1572 1544 1573 1574 1575 310 1576 1577 1249 ...
 - attr(*, "na.action")= 'omit' Named int [1:44] 27 218 286 413 417 769 855 1120 1242 1897 ...
  ..- attr(*, "names")= chr [1:44] "27" "218" "286" "413" ...

df2:

'data.frame':   301 obs. of  3 variables:
 $ Test_date : POSIXct, format: "2019-10-21 11:39:00" "2019-10-21 11:39:00" "2019-10-21 11:05:00" "2019-10-21 11:05:00" ...
 $ Value     : num  2.23 5.25 4.63 4.65 3.37 2.06 7.24 2.44 4.27 6.15 ...
 $ Patient_ID: int  1306 1280 1272 1230 1257 1328 1265 1301 1298 127 ...
 - attr(*, "na.action")= 'omit' Named int [1:139] 10 20 61 125 131 187 223 254 293 298 ...
  ..- attr(*, "names")= chr [1:139] "10" "20" "61" "125" ...
2 Answers

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)

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
Related