2

注意:我对 SQL 提出的类似问题 -如何使用窗口函数来确定何时在 Hive 或 Postgres 中执行不同的任务?

数据

我有一些数据显示了每人不同优先任务的开始日期和结束日期:

   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

注意:任务键是有序的,因此较高的字母具有较高的优先级。

问题

我需要确定每个人每天应该完成的任务,条件是:

  1. 字母较高的任务优先于字母较低的任务。
  2. 如果较高字母的任务与较低字母任务的任何部分重叠,则较低字母的任务将设置为 NA(表示此人永远不应从事该任务)。

简化

在实际数据中,原始表中的 end_day 始终为 5,即只有 start_day 变化,而 end_day 是恒定的。这意味着我想要的输出将具有与原始表相同的行数:)

输出

这是我需要的那种输出(Jason 更能代表我拥有的数据,它可以是涵盖 90 天的 100 多个任务):

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

最初的想法

有效,但我想要一个使用 dbplyr 包函数的解决方案,并且通常比这更好:

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

跟进问题

最终我需要在 SQL 中执行此操作,但这似乎太难了。我听说“dbply”包可以帮助我,因为如果我可以使用 dplyr 函数解决这个问题,那么它会以某种方式将其转换为有效的 SQL 查询?

4

2 回答 2

1

使用包的解决方案。map2unnest扩展数据集。arrange(person, desc(task_key))并且distinct(person, Days, .keep_all = TRUE)将根据 的顺序删除重复项task_key。之后,我们可以使用slice选择最后一行并操作开始和结束日期。

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
于 2018-02-03T16:30:44.360 回答
0

有趣的是,我不得不在本周早些时候做类似的事情,但在不同的背景下。

下面介绍了仅使用包的解决方案dplyr(第 10 步有警告,但我认为可以忽略)。

在将此dplyr解决方案转换为dbplyr具有相关有效 SQL 代码的解决方案方面,我不知道该怎么做(我试了一下,但没有用)。

编辑:在您问题的原始版本中,您的任务键使用数字而不是字母,这是我使用的。直到我发布后,我才看到您编辑了您的问题:)

带注释的代码:

# 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)

输出:

# 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
于 2018-02-04T14:54:21.290 回答