I am looking to calculate occupancy in emergency department (ED) with tidyverse. Occupancy is understood here in this particular problem as Admitted but did not leave the hospital within the same hour they were admitted. A clearer example would be: if I came at ED at 12:00:00 and did not leave within the hour I was admitted, then I am occupying the bospital. So for this I need to create a new column Occupancy. (A little insight to give - I want to plot occupancy by hour of the day. Yet I know how to plot this, but do not know how to calculate occupancy. Thus no need for you to be bogged down on this issue as I am giving you an insight of my project). What I need though is to learn how to calculate occupancy from the table I have bellow. Please do help.
I have ID, Admission = Adm and Disc = Discharges.
ID = c(101, 102,103, 104, 105, 106, 107)
Adm = as.POSIXct(c("2012-01-12 00:52:00", "2012-01-12 00:55:00", "2012-02-12
01:35:00", "2012-02-12 03:24:00", "2012-02-12 04:24:00",
"2012-02-12 05:24:00", "2012-02-12 05:28:00"))
Disc = as.POSIXct(c("2012-01-12 02:00:00", "2012-01-12 02:59:00", "2012-01-12
03:01:00", "2012-01-12 05:01:00", "2012-01-12 06:01:00",
"2012-01-12 08:01:00", "2012-01-12 08:01:00"))
df = data.frame(ID, Adm, Disc)
I have extracted the hour from the Admission. So that I can use the new column for calculating the occupancy - understood at the problem at hand as Admitted but were not discharged within the hour the patients were admitted. To remind you, I want to do this with tidyverse library
df_hour <- df %>%
mutate(Hour_Adm = lubridate::hour(as.POSIXct(Adm, "%Y%m%d %H:%M:%S")))
Any help is very much appreciated. Thank you.
Logic is to add 1 hour (i.e.
60*60
seconds) toAdm
time (which is ofPOSIXct
type) and compare it withDisc
time.First
&last
is added for cases wherein multiple rows are there for anID
. Then the earliestAdm
and latestDisc
time will only be considered perID
.which gives
Sample data:
We can try
The correct answer I hope so: