Created
August 2, 2019 15:06
-
-
Save gabrielburcea/8997975af16b7cfda2477808894a9ab6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
occupancy_fct <- function(start_date = as.Date("2016-06-01", tz = "Europe/London"), | |
end_date = as.Date("2016-06-08", tz = "Europe/London"), | |
data){ | |
dt_los <- data %>% | |
dplyr::select(spell_number, spell_start, spell_end) %>% | |
dplyr::mutate(Same_day_discharge = as.numeric(difftime(spell_end, spell_start, unit = c("min")))) | |
dt_calc <- dt_los %>% | |
dplyr::mutate(Same_day_discharge = dplyr::if_else(as.Date(spell_start) == as.Date(spell_end), TRUE, FALSE), | |
Los = as.numeric(difftime(spell_end, spell_start, unit = c("hour"))), | |
Discharged_24hr = dplyr::if_else(Los < 24, TRUE, FALSE)) %>% | |
dplyr::filter(Discharged_24hr == TRUE & Los <= 24) %>% | |
dplyr::filter(spell_start > start_date & spell_end < end_date) | |
# using gather function to create a new column with date | |
occupancy <- dt_calc %>% | |
tidyr::gather(key = type, time, spell_start:spell_end) %>% | |
dplyr::mutate(change = dplyr::if_else(type == "spell_start", 1, -1)) %>% | |
dplyr::group_by(time_hr = lubridate::floor_date(time, "1 hour")) %>% | |
dplyr::summarise(change = sum(change), | |
arrivals = sum(type == "spell_start")) %>% | |
padr::pad(start_val = start_date, end_val = end_date) %>% | |
tidyr::replace_na(list(arrivals = 0, change = 0)) %>% | |
dplyr::mutate(occupancy = cumsum(change)) %>% | |
tidyr::drop_na() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment