Created
July 10, 2023 10:19
-
-
Save Lextuga007/0e2cfc1e1ba2dec5a3b031896c19bcab to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| library(dplyr) | |
| library(tidyr) | |
| # Create the data - reduced the dates in timescale to make them easier to run and see the results | |
| data <- tibble::tribble( | |
| ~Doctor_name, ~Start_date, ~End_date, ~Weekly_hours, | |
| "Tom", "2023-07-01", "2023-07-06", 40, | |
| "Dick", "2023-06-01", "2023-08-30", 20, | |
| "Harry", "2023-05-01", "2023-07-14", 8 | |
| ) |> | |
| mutate(Start_date = lubridate::ymd(Start_date), | |
| End_date = lubridate::ymd(End_date), | |
| daily_hours = Weekly_hours/5) |> # Huge assumption here that every day in a working week is the same | |
| pivot_longer(cols = c(Start_date, | |
| End_date), | |
| names_to = "periods", | |
| values_to = "dates") | |
| # Expand the data set to include dates between start and end and then fill down the daily 5 day week worked hours | |
| df <- data |> | |
| group_by(Doctor_name) |> | |
| tidyr::complete(dates = seq(min(dates), max(dates), by = "day")) |> | |
| tidyr::fill(c(daily_hours), | |
| .direction = "down") |> | |
| ungroup() | |
| # Assume that weekends are not worked and remove Saturday and Sundays | |
| df_weekdays <- df |> | |
| filter(!(weekdays(as.Date(dates)) %in% c('Saturday','Sunday'))) | |
| df_weekdays |> | |
| filter(dates == "2023-07-03") | |
| summarise(daily_sum = sum(daily_hours), .by = dates) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment