Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. library(lubridate)
  2.  
  3. df1 <- data.frame("Name" = c("Adams", "Adams", "Adams", "Adams", "Ball", "Ball", "Cash", "Cash", "David", "David"),
  4. "Date.of.Service" = ymd(c("2005-10-01", "2005-10-01", "2005-10-01", "2005-10-02", "2005-10-01", "2005-10-01", "2005-10-01", "2005-10-02", "2005-10-01", "2005-10-02")),
  5. "StartTime" = c(845, 955, 2333, 0300, 1045, 1322, 1145, 344, 858, 123),
  6. "Code" = c("101", "500", "103", "104", "501", "103", "102", "106", "102", "109"))
  7.  
  8. df2 <- data.frame("Name" = c("Adams", "Adams", "Ball", "Cash", "Cash", "David", "David"),
  9. "Date.of.Shift" = ymd(c("2005-10-01", "2005-10-01", "2005-10-01", "2005-10-01", "2005-10-01", "2005-10-01", "2005-10-01")),
  10. "Shift" = c("CVCALL", "ORD", "OB", "ORD2", "OB", "SUP", "OB"),
  11. "Day.Night.Shift" = c("Full24", "Full24", "Day", "Day", "Night", "Day", "Full24"))
  12.  
  13. library(dplyr)
  14.  
  15. Heart.Codes <- c("500", "501")
  16.  
  17. df3 = df1 %>%
  18. # Bring in matching records in availability points. Filter df2 to records that are either
  19. # (1) the only record for that person, or (2) CV shifts.
  20. left_join(df2 %>%
  21. group_by(Name, Date.of.Shift) %>%
  22. mutate(num.shifts = n()) %>%
  23. filter(num.shifts == 1 | Shift %in% c("CVCALL")),
  24. by = c("Name", "Date.of.Service" = "Date.of.Shift")) %>%
  25. # We want to keep Shift and ShiftDate for records from availability that are either
  26. # (1) the only record for that person, or (2) CV shifts that join to a
  27. # "heart" type in df1.
  28. mutate(Shift = case_when(num.shifts == 1 ~ Shift,
  29. Code %in% Heart.Codes & Shift == "CVCALL" ~ Shift,
  30. T ~ NA_integer_),
  31. Date.of.Shift = case_when(num.shifts == 1 ~ Date.of.Service,
  32. Code %in% Heart.Codes & Shift == "CVCALL" ~ Date.of.Service),
  33. Day.Night.Shift = case_when(num.shifts == 1 ~ Day.Night.Shift,
  34. Code %in% Heart.Codes & Shift == "CVCALL" ~ Day.Night.Shift)) %>%
  35. select(Name, Date.of.Service, StartTime, Code, Date.of.Shift, Shift, Day.Night.Shift) %>%
  36. # assign correct shift when there are two shifts. Filter df2 to records that have two shifts in a day.
  37. left_join(df2 %>%
  38. group_by(Name, Date.of.Shift) %>%
  39. mutate(num.shifts = n()) %>%
  40. filter(num.shifts == 2),
  41. by = c("Name", "Date.of.Service" = "Date.of.Shift")) %>%
  42. mutate(Shift = case_when(num.shifts == 2 & StartTime > 629 & StartTime < 1629 & Day.Night.Shift == "Day" ~ Shift,
  43. num.shifts == 2 & StartTime > 2059 & StartTime < 2359 & Day.Night.Shift == "Night" ~ Shift,
  44. T ~ NA_integer_),
  45. Date.of.Shift = case_when(num.shifts == 2 & StartTime > 629 & StartTime < 1629 & Day.Night.Shift == "Day" ~ Date.of.Shift,
  46. num.shifts == 2 & StartTime > 2059 & StartTime < 2359 & Day.Night.Shift == "Night" ~ Date.of.Shift)) %>%
  47. select(Name, Date.of.Service, StartTime, Code, Date.of.Shift, Shift, Day.Night.Shift) %>%
  48. # Bring in records whose shift date is the day before the case date.
  49. left_join(df2 %>%
  50. group_by(Name, Date.of.Shift) %>%
  51. mutate(ShiftDateOneDayLater = Date.of.Shift + 1),
  52. by = c("Name", "Date.of.Service" = "ShiftDateOneDayLater")) %>%
  53. # Keep Shift and Date of Shift only if StartTime is between 0000 and 0659.
  54. mutate(Shift = case_when(!is.na(Shift.x) ~ Shift.x,
  55. Start.Time > 0 & Start.Time < 659 ~ Shift.y),
  56. Date.of.Shift = case_when(!is.na(Date.of.Shift.x) ~ Date.of.Shift.x,
  57. Start.Time > 0 & Start.Time < 659 ~ Date.of.Shift.y)) %>%
  58. select(Name, Date.of.Service, StartTime, Code, Date.of.Shift, Shift, Day.Night.Shift)
  59.  
  60. df3 <- data.frame("Name" = c("Adams", "Adams", "Adams", "Adams", "Ball", "Ball", "Cash", "Cash", "David", "David"),
  61. "Date.of.Service" = ymd(c("2005-10-01", "2005-10-01", "2005-10-01", "2005-10-02", "2005-10-01", "2005-10-01", "2005-10-01", "2005-10-02", "2005-10-01", "2005-10-02")),
  62. "StartTime" = c(845, 955, 2333, 0300, 1045, 1322, 1145, 344, 858, 123),
  63. "Code" = c("101", "500", "103", "104", "501", "103", "102", "106", "102", "109"),
  64. "Date.of.Shift" = ymd(c("2005-10-01", "2005-10-01", "2005-10-01", "2005-10-01", "2005-10-01", "2005-10-01", "2005-10-01", "2005-10-01", NA, "2005-10-01")),
  65. "Shift" = c("ORD", "CVCALL", "ORD", "ORD", "OB", "OB", "ORD2", "OB", NA, "OB"),
  66. "Day.Night.Shift" = c("Full24", "Full24", "Full24", "Full24", "Day", "Day", "Day", "Night", NA, "Full24"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement