Home > Software design >  Append dataframe outputs from a function into a single dataframe
Append dataframe outputs from a function into a single dataframe

Time:11-13

I have a data set of people in hospital with a specific condition. Each run of the df_output funciton produces a dataframe with persons in hospital with the condtion.

Passing an integer to the day argument e.g. will return everyone in hospital yesterday with the condition. I know there is likely a better way to do this but I'm currently trying to translate someone elses SQL to R code.

What I want to achieve is to iterate the function and have all the outputs combined to a single data froms. For example form the data and code below, achive the equivelent of: rbind(df_output(df, 1), df_output(df,2), df_output(df, 3)) up to and included df_output(df, n)

Here is a sample data set.

df <- structure(list(id = c(5625021, 14829441, 6471124, 20084005, 344096, 
6193816, 25080690, 25868218, 5782872, 4824459, 513300, 22103547, 
864055, 4685219, 15366903, 20822541, 2965349, 5048463, 5880372, 
10915925, 18085162, 22180920, 19151484, 11782765, 3986292, 3324277, 
18164080, 1526559, 21221347, 22181332, 24991817, 3514664, 4929016, 
19709185, 5901294, 20819851, 19819382, 6096341, 14136404, 21041970, 
3985328, 19019657, 18444, 4902916, 2300328, 155918, 21619178, 
23695417, 4679436, 3906352, 21488014, 5147560, 5690603, 3876397, 
10819536, 3690712, 6030691, 609203, 12154887, 884415, 5237114, 
355823, 6308595, 23410360, 4425559, 3258804, 2542992, 23269495, 
46512, 6259949, 6039988, 20175200, 17742264, 22916457, 85771, 
24769877, 4584683, 301481, 875648, 23574983, 47632, 21966698, 
1498049, 11276989, 21863376, 14703617, 19714927, 3021472, 12930503, 
1868265, 17377605, 3926699, 22365192, 3000256, 21152536, 23680057, 
4612644, 22262810, 20309637, 5755155, 3431719, 5226404, 22492624, 
5347765, 11100531, 21735744, 3122582, 6630411, 4253068, 25782643, 
21975522, 6629052, 2068566, 5952081, 3699375, 1087769, 12158795, 
14955070, 23580679, 23520383, 13224180, 5796752, 3878425, 23582247, 
3293838, 18678510, 20309637, 20654019, 6654150, 509649, 5879052, 
12005710, 24238, 21391247, 5791496, 4663981, 20406663, 19942469, 
2617740, 1185447, 13665867, 21022505, 20414578, 4572889, 26021577, 
22021439, 5012968, 3430809, 6735016, 20830144, 17983754, 22716109, 
6194949, 23764130, 17880314, 20807592, 11745, 2724059, 3539449, 
12483971, 74447, 6279987, 18368134, 1095434, 15170339, 1773696, 
5841971, 24828324, 18193196, 16687954, 2695685, 3917502, 20750135, 
6039667, 5848198, 24993406, 24232037, 10818362, 17092487, 18761259, 
91686, 3967857, 20283074, 18085869, 20359, 3125153, 6203769, 
5843978, 5832513, 885235, 21074516, 19851597, 20543120, 20638399, 
96329, 21711744, 305589, 4425559, 4987550, 6759900, 20283170, 
2738606, 21643240, 5955548, 2756009, 3346137, 20251868, 5369820, 
1190273, 1720688, 20859682, 22826753, 20808354, 605995, 19954453, 
13249757, 100461, 4788097, 4011807, 5770155, 645024, 1692635, 
5848167, 5625516, 2931584, 32008, 4987550, 22354994, 13764992, 
16521128, 26053688, 19805204, 6640288, 14470141, 1770575, 1088451, 
2911803, 3172003, 22807712, 21262331, 17713817, 2057239, 5863089, 
22354994, 19332748, 483467, 23683089, 888058, 21941138, 138917
), AdmissionDate = structure(c(1668063600, 1666342080, 1667010600, 
1667624820, 1666395000, 1667044740, 1666466520, 1667559840, 1667482920, 
1666337640, 1666751289, 1666004640, 1668166140, 1665993600, 1666875600, 
1666013400, 1667214180, 1667546520, 1667242800, 1667301060, 1666273380, 
1666704060, 1666191300, 1666612080, 1667296620, 1667374560, 1667554920, 
1668101400, 1666765800, 1667806200, 1666088160, 1666249560, 1667376720, 
1666385940, 1667622000, 1666166100, 1667566260, 1666711200, 1666265460, 
1667288400, 1665764700, 1666287600, 1666911600, 1666069080, 1667319420, 
1667512020, 1667291640, 1666965900, 1665877260, 1667029200, 1666584300, 
1667461800, 1666549560, 1667825460, 1667659560, 1668092820, 1666955940, 
1666398120, 1666091280, 1666423200, 1666592760, 1667372520, 1667393940, 
1666003320, 1666643460, 1666615440, 1667977980, 1667575800, 1666361160, 
1666077000, 1668148200, 1666425960, 1667890140, 1667584860, 1666937700, 
1667546100, 1668093600, 1667484000, 1668002400, 1666224660, 1665817200, 
1667298600, 1666632600, 1666869420, 1667377980, 1666248600, 1666777500, 
1667977560, 1666855140, 1666256760, 1666716960, 1667766840, 1667204340, 
1667097600, 1667058780, 1667222880, 1666083720, 1665870300, 1666366200, 
1667997780, 1666370280, 1666016940, 1666263900, 1667551800, 1667479380, 
1666366080, 1666408740, 1666609620, 1666174200, 1667926800, 1667230200, 
1667804400, 1666542780, 1666041480, 1666858560, 1667595600, 1665879720, 
1666612080, 1666914600, 1666897680, 1665806820, 1667904300, 1667848800, 
1667845800, 1666698540, 1667455200, 1668152460, 1667826480, 1668062700, 
1665998100, 1667567640, 1667504940, 1665961200, 1667213460, 1668175800, 
1666015200, 1666425600, 1665848520, 1666743540, 1665783000, 1667752800, 
1666775400, 1666166460, 1666684800, 1667783820, 1666308840, 1666941000, 
1666441020, 1667300760, 1666101540, 1667902620, 1668155160, 1666662240, 
1666782900, 1667201160, 1665946680, 1667787000, 1668147600, 1666157880, 
1668240000, 1666447200, 1666169700, 1667824620, 1666854900, 1667370600, 
1667298000, 1667894400, 1667889600, 1666344120, 1667217780, 1667809620, 
1667224800, 1666611540, 1666769340, 1666968300, 1666976880, 1668163980, 
1666618500, 1667384160, 1667979000, 1666365300, 1666599180, 1666868280, 
1666708200, 1665814860, 1666765800, 1666165380, 1668000600, 1667820300, 
1667222940, 1666531140, 1666615800, 1666445040, 1666891380, 1665991200, 
1666872000, 1666967400, 1666971780, 1666253640, 1666984020, 1667821260, 
1667405700, 1668096300, 1667995560, 1666626360, 1667983380, 1667700000, 
1666447980, 1666445400, 1666052400, 1666593900, 1665844200, 1667226000, 
1666264020, 1668067200, 1667100180, 1665937200, 1667635200, 1667897648, 
1666422900, 1666626000, 1667177820, 1668262500, 1665990600, 1667223300, 
1666248300, 1667718540, 1667201700, 1666252200, 1666180200, 1667883120, 
1668091980, 1668151800, 1668072300, 1667808000, 1667049600, 1667719800, 
1668062400, 1667026800, 1665817200, 1667222400, 1667528400, 1666086900, 
1667548200, 1667375220, 1665787200, 1667995200, 1666656180, 1666633440, 
1668243420), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
    DischargeDate = c("2022-11-10 10:30:00.000", "2022-10-21 15:51:00.000", 
    "2022-10-29 19:00:00.000", "2022-11-07 15:13:32.000", "2022-10-22 18:25:37.000", 
    "NULL", "2022-10-24 14:11:00.000", "2022-11-05 17:25:28.000", 
    "2022-11-03 18:38:00.000", "2022-10-21 13:03:42.000", "2022-10-26 10:20:00.000", 
    "2022-10-17 13:05:00.000", "NULL", "2022-10-17 09:35:00.000", 
    "2022-10-27 15:32:00.000", "2022-10-17 19:34:11.000", "2022-11-01 14:35:00.000", 
    "2022-11-12 08:50:00.000", "NULL", "2022-11-01 20:40:00.000", 
    "2022-10-20 15:27:00.000", "2022-10-25 16:10:00.000", "2022-10-19 16:13:00.000", 
    "2022-10-24 16:16:51.000", "2022-11-01 11:45:48.000", "2022-11-02 12:38:27.000", 
    "2022-11-04 16:27:00.000", "2022-11-11 09:24:23.000", "2022-10-26 11:15:00.000", 
    "2022-11-07 12:00:00.000", "2022-10-18 18:00:00.000", "2022-10-21 10:15:00.000", 
    "2022-11-02 14:45:00.000", "2022-10-24 17:13:41.000", "2022-11-05 11:06:00.000", 
    "2022-10-19 13:45:00.000", "2022-11-04 14:04:39.000", "2022-10-25 15:47:54.000", 
    "NULL", "2022-11-06 14:00:00.000", "2022-10-20 14:20:00.000", 
    "2022-10-22 07:55:00.000", "2022-10-30 12:00:00.000", "2022-10-18 09:11:00.000", 
    "2022-11-07 16:00:00.000", "2022-11-05 12:30:00.000", "2022-11-01 12:20:00.000", 
    "2022-10-28 18:35:00.000", "2022-10-16 15:02:00.000", "2022-10-29 12:40:00.000", 
    "2022-10-25 16:22:38.000", "2022-11-09 13:30:00.000", "2022-11-01 12:15:00.000", 
    "2022-11-08 16:30:00.000", "2022-11-05 19:37:27.000", "2022-11-10 20:07:58.000", 
    "2022-11-02 10:00:00.000", "2022-10-28 13:03:42.000", "2022-10-18 15:49:00.000", 
    "2022-10-22 13:25:00.000", "2022-10-24 10:31:45.000", "2022-11-02 14:18:00.000", 
    "2022-11-02 16:50:00.000", "2022-10-18 13:29:06.000", "2022-10-25 06:52:00.000", 
    "2022-10-24 17:42:14.000", "2022-11-09 14:00:00.000", "2022-11-04 18:00:00.000", 
    "2022-10-21 15:36:54.000", "2022-10-18 12:40:00.000", "2022-11-11 12:05:02.000", 
    "2022-10-22 13:35:07.000", "NULL", "2022-11-07 16:56:24.000", 
    "2022-10-28 11:40:00.000", "2022-11-05 13:23:13.000", "2022-11-11 13:00:00.000", 
    "2022-11-03 21:00:00.000", "2022-11-09 19:30:00.000", "2022-10-25 13:04:24.000", 
    "2022-10-15 11:30:00.000", "2022-11-02 15:21:00.000", "2022-10-24 22:30:00.000", 
    "2022-10-29 17:00:00.000", "2022-11-03 09:03:07.000", "2022-10-20 11:40:00.000", 
    "2022-10-26 11:52:00.000", "2022-11-09 11:33:23.000", "2022-10-28 09:35:00.000", 
    "NULL", "2022-10-25 19:13:00.000", "NULL", "2022-10-31 09:18:00.000", 
    "2022-10-30 10:02:20.000", "2022-10-29 17:45:00.000", "2022-10-31 14:40:00.000", 
    "2022-10-18 12:45:00.000", "2022-10-16 08:25:00.000", "2022-10-24 06:00:00.000", 
    "2022-11-11 17:30:00.000", "2022-10-22 15:00:00.000", "2022-10-17 15:42:00.000", 
    "2022-11-04 16:07:00.000", "2022-11-05 18:00:00.000", "2022-11-03 13:55:00.000", 
    "2022-10-21 19:58:00.000", "2022-10-26 12:15:00.000", "2022-10-25 10:05:00.000", 
    "2022-10-20 09:25:54.000", "2022-11-11 16:55:00.000", "2022-11-02 09:05:00.000", 
    "2022-11-07 12:00:00.000", "2022-10-23 19:15:00.000", "2022-10-19 12:00:00.000", 
    "2022-10-27 11:45:00.000", "2022-11-07 13:00:00.000", "2022-10-16 03:00:00.000", 
    "2022-10-24 14:43:00.000", "2022-10-28 04:44:00.000", "2022-10-28 08:30:00.000", 
    "2022-10-15 15:48:46.000", "2022-11-08 11:30:00.000", "2022-11-08 05:15:00.000", 
    "NULL", "2022-10-25 16:33:44.000", "2022-11-03 12:14:43.000", 
    "2022-11-11 12:43:00.000", "2022-11-07 16:50:00.000", "2022-11-10 08:00:00.000", 
    "2022-10-17 16:15:00.000", "2022-11-04 16:40:00.000", "2022-11-04 14:05:00.000", 
    "2022-10-18 16:00:00.000", "2022-10-31 16:15:50.000", "2022-11-11 19:30:00.000", 
    "2022-10-17 19:30:00.000", "2022-10-22 13:30:00.000", "2022-10-15 17:19:00.000", 
    "2022-10-26 10:45:00.000", "2022-10-15 12:15:00.000", "NULL", 
    "2022-10-26 09:56:00.000", "NULL", "2022-10-25 13:30:00.000", 
    "2022-11-08 08:01:26.000", "2022-10-22 16:59:04.000", "2022-10-28 12:40:17.000", 
    "2022-10-22 19:00:00.000", "2022-11-01 13:31:00.000", "2022-10-18 18:55:00.000", 
    "2022-11-08 19:30:00.000", "2022-11-11 12:45:00.000", "2022-10-26 19:25:00.000", 
    "2022-11-07 09:00:00.000", "2022-10-31 12:40:26.000", "2022-10-16 20:55:45.000", 
    "NULL", "2022-11-11 11:05:00.000", "2022-10-20 08:59:12.000", 
    "2022-11-12 12:00:00.000", "2022-10-22 21:00:00.000", "2022-10-19 13:50:00.000", 
    "2022-11-07 14:09:00.000", "2022-10-27 12:30:00.000", "2022-11-02 12:00:00.000", 
    "2022-11-04 12:44:35.000", "2022-11-08 13:30:00.000", "2022-11-10 14:15:57.000", 
    "2022-10-21 15:22:00.000", "2022-11-08 15:13:00.000", "2022-11-07 13:27:40.000", 
    "2022-10-31 19:22:27.000", "2022-10-24 13:00:00.000", "2022-10-26 11:30:00.000", 
    "2022-10-28 16:20:00.000", "2022-10-28 18:15:00.000", "2022-11-11 13:50:00.000", 
    "2022-10-24 19:37:56.000", "2022-11-06 11:04:24.000", "2022-11-09 13:33:14.000", 
    "2022-10-21 20:40:00.000", "2022-10-24 12:56:07.000", "2022-10-27 12:00:00.000", 
    "2022-10-25 18:40:00.000", "2022-10-17 02:42:00.000", "2022-10-26 12:50:28.000", 
    "2022-10-19 16:55:00.000", "2022-11-09 18:20:00.000", "2022-11-07 18:30:00.000", 
    "2022-10-31 15:15:00.000", "2022-10-23 14:22:00.000", "2022-10-25 13:30:00.000", 
    "2022-10-22 18:55:00.000", "2022-11-02 15:54:00.000", "2022-10-17 12:45:00.000", 
    "2022-10-27 20:30:00.000", "2022-10-28 20:25:00.000", "2022-10-29 12:55:25.000", 
    "2022-10-20 10:00:00.000", "2022-11-02 14:42:00.000", "2022-11-07 16:33:49.000", 
    "2022-11-03 08:45:00.000", "2022-11-12 22:04:00.000", "2022-11-09 17:06:00.000", 
    "2022-10-27 14:09:20.000", "2022-11-09 12:01:36.000", "2022-11-06 10:08:05.000", 
    "2022-10-22 19:00:00.000", "2022-10-22 18:30:00.000", "2022-10-18 12:30:36.000", 
    "2022-10-24 12:15:00.000", "2022-10-15 20:20:00.000", "2022-10-31 15:37:36.000", 
    "2022-10-20 16:50:00.000", "2022-11-10 13:30:00.000", "2022-10-30 06:45:00.000", 
    "2022-10-20 14:19:24.000", "2022-11-05 12:51:51.000", "2022-11-09 16:45:00.000", 
    "2022-10-22 13:00:00.000", "2022-10-25 17:00:00.000", "2022-11-04 17:00:00.000", 
    "NULL", "NULL", "2022-10-31 14:10:00.000", "2022-10-20 12:20:00.000", 
    "2022-11-07 18:23:00.000", "2022-10-31 13:42:31.000", "2022-10-20 13:00:00.000", 
    "2022-10-19 19:16:36.000", "NULL", "2022-11-10 17:20:00.000", 
    "NULL", "2022-11-10 11:33:14.000", "2022-11-07 13:30:00.000", 
    "2022-10-29 18:05:00.000", "2022-11-06 10:00:00.000", "2022-11-10 11:09:24.000", 
    "2022-10-29 12:44:00.000", "2022-10-15 13:00:00.000", "2022-10-31 17:02:34.000", 
    "2022-11-04 06:07:00.000", "2022-10-18 13:05:00.000", "2022-11-04 13:50:48.000", 
    "2022-11-02 08:19:00.000", "2022-10-16 11:35:00.000", "2022-11-12 11:00:00.000", 
    "2022-10-27 13:55:32.000", "2022-10-24 19:57:00.000", "2022-11-12 14:52:57.000"
    ), EpisodeDate = structure(c(19015, 19028, 19025, 19019, 
    19042, 19305, 19236, 19300, 19016, 19102, 19148, 19122, 18990, 
    19207, 19242, 19114, 19026, 19215, 19151, 19011, 19007, 19125, 
    18999, 19115, 19217, 19132, 18993, 19114, 19128, 19184, 19233, 
    19172, 19240, 19005, 19041, 19107, 19009, 19016, 19299, 19060, 
    19093, 19024, 19283, 19136, 19298, 19065, 19090, 19181, 19089, 
    19011, 19236, 19282, 19301, 19031, 19006, 19044, 19283, 19226, 
    19023, 19026, 19027, 19140, 19199, 19171, 19110, 18997, 19293, 
    19166, 19205, 19098, 19200, 19178, 19008, 19153, 19265, 19221, 
    19022, 19011, 19188, 19210, 19201, 19117, 19100, 19094, 19108, 
    19053, 19001, 19102, 19047, 19008, 19188, 19302, 19134, 19232, 
    19005, 19181, 19129, 19209, 19030, 19305, 19184, 19104, 19099, 
    19095, 19099, 19099, 19039, 19029, 19242, 19264, 19133, 19163, 
    19288, 19190, 19175, 19015, 19280, 19033, 19177, 19248, 19084, 
    19139, 19100, 19177, 19124, 18998, 19030, 19016, 19009, 19085, 
    19110, 19130, 19083, 19207, 19036, 19030, 19094, 19010, 19091, 
    19109, 19226, 19058, 19185, 19003, 19302, 19115, 19184, 19142, 
    19000, 19199, 19040, 19198, 19290, 19274, 19223, 19042, 19222, 
    19002, 18345, 19255, 19125, 19116, 18995, 19173, 19025, 19018, 
    19063, 19225, 18993, 18998, 19083, 18995, 19037, 18999, 19196, 
    19183, 19200, 19200, 19156, 19011, 19186, 19090, 19146, 19004, 
    19086, 19006, 18997, 19150, 18987, 19049, 19056, 19092, 19029, 
    19124, 19236, 19191, 19184, 19110, 19301, 19093, 19019, 19241, 
    19092, 19019, 19049, 19124, 19012, 18987, 19257, 19210, 19184, 
    19194, 19000, 19092, 19009, 19204, 18994, 19004, 19290, 19185, 
    19009, 18999, 19198, 19107, 19009, 19206, 19090, 19254, 19105, 
    19009, 19305, 19006, 18990, 19216, 19149, 19151, 19254, 19143, 
    19150, 19133, 18989, 19013, 19016, 19254, 19027, 18996, 19177, 
    18996, 19037, 19287), class = "Date")), row.names = c(NA, 
-250L), class = c("tbl_df", "tbl", "data.frame"))


Here is my function:


df_output <- function(df, day) {

    REPORT_DATE = today() - day
    df %>% 
        mutate(DischargeDate = ifelse(DischargeDate == "NULL", as.character(now()), DischargeDate))%>% 
        mutate(DischargeDate = ymd_hms(DischargeDate)) %>%
        mutate(time_in_hospital =  time_length(interval(AdmissionDate, DischargeDate), unit = "day")) %>% 
        mutate(report_date  = REPORT_DATE) %>% 
        filter(AdmissionDate >= (EpisodeDate -14) | AdmissionDate <= (EpisodeDate   14)) %>% 
        filter(AdmissionDate <= report_date & DischargeDate >= report_date) %>% 
        filter(time_in_hospital >= 1) %>% 
        mutate(time_with_condition =  ifelse(as_date(AdmissionDate) <= EpisodeDate,
                                         time_length(interval(AdmissionDate, DischargeDate), unit = "day"), 
                                         time_length(interval(EpisodeDate, DischargeDate), unit = "day"))) %>% 
        select(id, AdmissionDate, EpisodeDate, DischargeDate, report_date, time_with_condition) %>%
        filter(time_with_condition <= 14)

}

CodePudding user response:

Here's a wrapper function that will apply your function to the data frame for all days from 1 to n, add the output df to a list, and then rbind() all those together into the one dataframe.

rbind_df_output <- function(df, n) {
  l <- lapply(seq_along(n), function(x) df_output(df, x))
  do.call(rbind, l)
}

For your example you would do

rbind_df_output(df, 3)
  • Related