How to use a window function to determine when to perform different tasks?

两盒软妹~` 提交于 2019-12-04 18:02:22

A solution using the 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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!