Skip to content

Instantly share code, notes, and snippets.

@brshallo
Created November 14, 2018 23:55
Show Gist options
  • Save brshallo/597b37ee264b025ae2b72e6b32a5afb2 to your computer and use it in GitHub Desktop.
Save brshallo/597b37ee264b025ae2b72e6b32a5afb2 to your computer and use it in GitHub Desktop.
Given question: provided a dataset with employee id, clock in, and clock out data... how would you get hourly counts of employees on the clock?
# Libraries
library(tidyverse, quietly = TRUE)
library(lubridate, warn.conflicts = FALSE, quietly = TRUE)
# Create simulated data set
set.seed(123)
possible_starts <- seq.POSIXt(ymd_hms("2018-10-30 09:00:00"), ymd_hms("2018-10-30 14:00:00"), by = "min")
possible_ends <- seq.POSIXt(ymd_hms("2018-10-30 14:00:00"), ymd_hms("2018-10-30 20:00:00"), by = "min")
(work_times <- tibble(worker_id = 1:100,
start = sample(possible_starts, 100, TRUE),
end = sample(possible_ends, 100, TRUE)))
#> # A tibble: 100 x 3
#> worker_id start end
#> <int> <dttm> <dttm>
#> 1 1 2018-10-30 10:26:00 2018-10-30 17:36:00
#> 2 2 2018-10-30 12:57:00 2018-10-30 16:00:00
#> 3 3 2018-10-30 11:03:00 2018-10-30 16:56:00
#> 4 4 2018-10-30 13:25:00 2018-10-30 19:44:00
#> 5 5 2018-10-30 13:43:00 2018-10-30 16:54:00
#> 6 6 2018-10-30 09:13:00 2018-10-30 19:21:00
#> 7 7 2018-10-30 11:38:00 2018-10-30 19:30:00
#> 8 8 2018-10-30 13:28:00 2018-10-30 17:39:00
#> 9 9 2018-10-30 11:45:00 2018-10-30 16:28:00
#> 10 10 2018-10-30 11:17:00 2018-10-30 14:53:00
#> # ... with 90 more rows
# Create expanded vectors for hours-->unnest-->group-->count
(hourly_counts <- work_times %>%
mutate_if(is.POSIXt, funs(floor_date(., unit = "hours"))) %>%
mutate(hours_worked = map2(start, end, seq.POSIXt, by = "hour")) %>%
unnest(hours_worked) %>%
count(hours_worked))
#> # A tibble: 11 x 2
#> hours_worked n
#> <dttm> <int>
#> 1 2018-10-30 09:00:00 19
#> 2 2018-10-30 10:00:00 39
#> 3 2018-10-30 11:00:00 60
#> 4 2018-10-30 12:00:00 81
#> 5 2018-10-30 13:00:00 100
#> 6 2018-10-30 14:00:00 100
#> 7 2018-10-30 15:00:00 90
#> 8 2018-10-30 16:00:00 70
#> 9 2018-10-30 17:00:00 50
#> 10 2018-10-30 18:00:00 29
#> 11 2018-10-30 19:00:00 18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment