Note: Similar question I have asked for SQL - How to use a window function to determine when to perform different tasks in Hive or Postgres?
Data
I have a some data showing the start day and end day for different pre-prioritised tasks per person:
input_df <- data.frame(person = c(rep("Kate", 2), rep("Adam", 2), rep("Eve", 2), rep("Jason", 5)),
task_key = c(c("A","B"), c("A","B"), c("A","B"), c("A","B","C","D","E")),
start_day = c(c(1L,1L), c(1L,2L), c(2L,1L), c(1L,4L,3L,5L,4L)),
end_day = 5L)
person task_key start_day end_day
1 Kate A 1 5
2 Kate B 1 5
3 Adam A 1 5
4 Adam B 2 5
5 Eve A 2 5
6 Eve B 1 5
7 Jason A 1 5
8 Jason B 4 5
9 Jason C 3 5
10 Jason D 5 5
11 Jason E 4 5
NOTE: Task key is ordered so that higher letters have higher priorities.
Question
I need to work out which task each person should be working on each day, with the condition that:
- Higher lettered tasks take priority over lower lettered tasks.
- If a higher lettered task overlaps any part of a lower lettered task, then the lower lettered task gets set to NA (to represent that the person should not work on it ever).
Simplification
In the real data the end_day is always 5 in the original table i.e. only the start_day varies but the end_day is constant. This means my desired output will have the same number of rows as my original table :)
Output
This is the sort of output I need (Jason is more representative of the data I have which can be over 100 tasks covering a period of 90 days):
output_df <- data.frame(person = c(rep("Kate", 2), rep("Adam", 2), rep("Eve", 2), rep("Jason", 5)),
task_key = c(c("A","B"), c("A","B"), c("A","B"), c("A","B","C","D","E")),
start_day = c(c(1L,1L), c(1L,2L), c(2L,1L), c(1L,4L,3L,5L,4L)),
end_day = 5L,
valid_from = c( c(NA,1L), c(1L,2L), c(NA,1L), c(1L,NA,3L,NA,4L) ),
valid_to = c( c(NA,5L), c(2L,5L), c(NA,5L), c(3L,NA,4L,NA,5L) ))
person task_key start_day end_day valid_from valid_to
1 Kate A 1 5 NA NA
2 Kate B 1 5 1 5
3 Adam A 1 5 1 2
4 Adam B 2 5 2 5
5 Eve A 2 5 NA NA
6 Eve B 1 5 1 5
7 Jason A 1 5 1 3
8 Jason B 4 5 NA NA
9 Jason C 3 5 3 4
10 Jason D 5 5 NA NA
11 Jason E 4 5 4 5
Initial Thoughts
Works but I want a solution that works using the dbplyr package functions and something that is generally better than this:
tmp <- input_df %>% filter(person == "Jason")
num_rows <- nrow(tmp)
tmp$valid_from <- NA
tmp$valid_to <- NA
for(i in 1:num_rows) {
# Curent value
current_value <- tmp$start_day[i]
# Values to test against
vec <- lead(tmp$start, i)
# test
test <- current_value >= vec
# result
if(any(test, na.rm = TRUE) & i!=num_rows) {
tmp$valid_from[i] <- NA
tmp$valid_to[i] <- NA
} else if(i!=num_rows) {
tmp$valid_from[i] <- current_value
tmp$valid_to[i] <- min(vec, na.rm = TRUE)
} else {
tmp$valid_from[i] <- current_value
tmp$valid_to[i] <- max(tmp$end_day, na.rm = TRUE)
}
}
tmp
person task_number start_day end_day valid_from valid_to
1 Jason A 1 5 1 3
2 Jason B 4 5 NA NA
3 Jason C 3 5 3 4
4 Jason D 5 5 NA NA
5 Jason E 4 5 4 5
Follow up question
Eventually I'll need to do this in SQL but that seems too hard. I heard that the 'dbply' package could help me here because if I can solve this using the dplyr functions then it will somehow convert that to a valid SQL query?
A solution using the tidyverse package. map2
and unnest
are to expand the dataset. arrange(person, desc(task_key))
and distinct(person, Days, .keep_all = TRUE)
are to remove duplicates based on the order of task_key
. After that, we can use slice
to select the last row and manipulate the start and end dates.
library(tidyverse)
output_df <- input_df %>%
mutate(Days = map2(start_day, end_day, `:`)) %>%
unnest() %>%
arrange(person, desc(task_key)) %>%
distinct(person, Days, .keep_all = TRUE) %>%
arrange(person, task_key, Days) %>%
group_by(person, task_key) %>%
slice(n()) %>%
mutate(end_day = ifelse(Days < end_day, Days + 1L, end_day)) %>%
select(-Days) %>%
rename(valid_from = start_day, valid_to = end_day) %>%
right_join(input_df, by = c("person", "task_key")) %>%
select(names(input_df), starts_with("valid")) %>%
ungroup()
output_df
# # A tibble: 11 x 6
# person task_key start_day end_day valid_from valid_to
# <fct> <fct> <int> <int> <int> <int>
# 1 Kate A 1 5 NA NA
# 2 Kate B 1 5 1 5
# 3 Adam A 1 5 1 2
# 4 Adam B 2 5 2 5
# 5 Eve A 2 5 NA NA
# 6 Eve B 1 5 1 5
# 7 Jason A 1 5 1 3
# 8 Jason B 4 5 NA NA
# 9 Jason C 3 5 3 4
# 10 Jason D 5 5 NA NA
# 11 Jason E 4 5 4 5
Interestingly, I had to do something similar earlier in the week but in a different context.
A solution using just the dplyr
package is presented below (there is a warning at step 10 but I think it can be ignored).
In terms of converting this dplyr
solution into a dbplyr
solution with associated valid SQL code is something I don't know how to do (I gave it a go but it didn't work).
EDIT: In the original version of your question you had numbers instead of letters for your task key which is what I used. I didn't see you had edited your question until after I posted :)
Code with comments:
# Load packages.
library(DBI)
library(dplyr)
library(dbplyr)
library(RSQLite)
library(RPostgreSQL)
# Data
input_df <- data.frame(person = c(rep("Kate", 2), rep("Adam", 2), rep("Eve", 2), rep("Jason", 5)),
task_key = c(1:2, 1:2, 1:2, 1:5),
start_day = c(c(1L,1L), c(1L,2L), c(2L,1L), c(1L,4L,3L,5L,4L)),
end_day = 5L)
# [OPTIONAL] Convert to a databse; I couldn't figure out how to make an in-memory verson of PostgreSQL using RPostgreSQL::PostgreSQL()
# If this worked, then you could use the show_query() function to see the SQL it generates.
#con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
#DBI::dbWriteTable(con, "input_df", input_df)
#input_df <- tbl(con, "input_df")
# Step 01: Keep only minimal information.
df01 <- input_df %>%
select(person, task_key, start_day) %>%
distinct() %>%
dplyr::rename(tk=task_key, sd=start_day)
# show_query(df01)
# Step 02: Explode table with all pair-wise comparisons for each person.
df02 <- left_join(x = df01, y = df01, by = c("person"), suffix = c(".bas", ".alt"))
# show_query(df02)
# Step 03: Remove self-comparisons
df03 <- filter(.data = df02, tk.bas != tk.alt)
# show_query(df03)
# Step 04: Add a flag to indicate when the baseline task takes priority over the comparator.
df04 <- mutate(.data = df03, tk.bas_priority = tk.bas > tk.alt) # check inequality
# show_query(df04)
# Step 05: Add a flag to indicate when the baseline date is earlier then the comparator date.
df05 <- mutate(.data = df04, sd.bas_earliest = sd.bas < sd.alt)
# show_query(df05)
# Step 06: Is it possible to reduce the number of comparisons?
# I think there is a way but haven't looked into it.
df06 <- df05
# show_query(df06)
# Step 07: Organise columns to make them easier for me to read.
df07 <- select(.data = df06, person, tk.bas, tk.alt, tk.bas_priority, sd.bas, sd.alt, sd.bas_earliest)
# show_query(df07)
# Step 08: Group table by person and baseline date.
df08 <- group_by(.data = df07, person, tk.bas)
# show_query(df08)
# Step 09: Create start dates.
df09 <- df08 %>%
mutate(start = case_when(
tk.bas_priority == TRUE & sd.bas_earliest == TRUE ~ sd.bas,
tk.bas_priority == TRUE & sd.bas_earliest == FALSE ~ sd.bas,
tk.bas_priority == FALSE & sd.bas_earliest == TRUE ~ sd.bas,
tk.bas_priority == FALSE & sd.bas_earliest == FALSE ~ NA_integer_,
TRUE ~ -1L
)) %>%
mutate(start = as.integer(min(start, na.rm = FALSE)))
# show_query(df09)
# Step 10: Create end dates.
# Note: This will create warnings because empty vectors might be applied to 'min' or 'max'.
# I think these can be ignored because it doesn't matter in this case?
df10 <- df09 %>%
mutate(end = case_when(
tk.bas_priority == TRUE & sd.bas_earliest == TRUE ~ as.integer(max(sd.alt)),
tk.bas_priority == TRUE & sd.bas_earliest == FALSE ~ as.integer(max(sd.alt)),
tk.bas_priority == FALSE & sd.bas_earliest == TRUE ~ as.integer(min(sd.alt[tk.bas_priority == F])),
tk.bas_priority == FALSE & sd.bas_earliest == FALSE ~ NA_integer_,
TRUE ~ -1L
)) %>%
mutate(end = as.integer(min(end, na.rm = FALSE)))
# show_query(df10)
# Step 11: Ungroup table.
df11 <- ungroup(df10)
# show_query(df11)
# Step 12: Reduce table to distinct start/end values for each person and baseline ad.
df12 <- df11 %>%
select(person, tk.bas, start, end) %>%
distinct()
# show_query(df12)
# Step 13: Join back onto original data.
df13 <- left_join(input_df, df12, by = c("person"="person", "task_key"="tk.bas"))
# show_query(df13)
# Step 14: Account for the end date for the final row per person
df14 <- df13 %>%
group_by(person) %>%
mutate(end = if_else(row_number() == n(), as.integer(max(end_day)), end)) %>%
ungroup()
# show_query(df14)
# collect(df14)
Output:
# A tibble: 11 x 6
person task_key start_day end_day start end
<fct> <int> <int> <int> <int> <int>
1 Kate 1 1 5 NA NA
2 Kate 2 1 5 1 5
3 Adam 1 1 5 1 2
4 Adam 2 2 5 2 5
5 Eve 1 2 5 NA NA
6 Eve 2 1 5 1 5
7 Jason 1 1 5 1 3
8 Jason 2 4 5 NA NA
9 Jason 3 3 5 3 4
10 Jason 4 5 5 NA NA
11 Jason 5 4 5 4 5