如果声明和日期

时间:2018-11-19 02:45:17

标签: r date if-statement

我要实现的目标是分配一个新列,该列具有随时间变化的变量。 只是其中一个正在改变,所以我想做的是After(date)输入40而不是22

我当前有此代码无法正常工作

Idea:
    if(lst$taskDate <= as.Date("2018-11-18")){
    t2$Budget <- case_when(
        t2$taskStaffName == "L" ~ 20,
        t2$taskStaffName == "J" ~ 22,
        TRUE ~ 40
    )
    }else{
        if(lst$taskDate >= as.Date("2018-11-19"))
    t2$Budget <- case_when(
        t2$taskStaffName == "L" ~ 20,
        t2$taskStaffName == "J" ~ 40,
                TRUE ~ 40

    )
        }


This is the Data Sample:

# A tibble: 3,692 x 4
   taskStaffName taskDate   taskMinutes taskBillable
   <chr>         <date>     <chr>       <chr>       
 1 G             2018-07-02 300         true        
 2 G             2018-07-02 180         true        
 3 L             2018-07-02 300         true        
 4 L             2018-07-02 180         false       
 5 C             2018-07-02 360         false       
 6 C             2018-07-02 120         false       
 7 G             2018-07-03 480         true        
 8 L             2018-07-03 30          true        
 9 L             2018-07-03 180         true        
10 L             2018-07-02 30          true        
# ... with 3,682 more rows



Desired Outcome:

# A tibble: 3,692 x 5
   taskStaffName taskDate   taskMinutes taskBillable Budget
   <chr>         <date>     <chr>       <chr>         <dbl>
 1 J             2018-07-02 300         true             22
 2 J             2018-07-02 180         true             22
 3 L             2018-07-02 300         true             20
 4 L             2018-07-02 180         false            20
 5 C             2018-07-02 360         false            40
 6 C             2018-07-02 120         false            40
 7 L             2018-07-03 480         true             20
 8 L             2018-07-03 30          true             20
 9 J             2018-11-19 180         true             40
10 J             2018-11-19 30          true             40
# ... with 3,682 more rows

2 个答案:

答案 0 :(得分:1)

推断,您每个taskStaffName的前后费率会有所不同,因此最好重新考虑如何解决该问题。与其在名称和日期的每个组合中进行case_when(或更糟糕的是ifelse),不如合并在前/后费率框架中并使用适当的字段。

x <- read.table(header=TRUE, stringsAsFactors=FALSE, text="
   taskStaffName taskDate   taskMinutes taskBillable
 1 G             2018-07-02 300         true        
 2 G             2018-07-02 180         true        
 3 L             2018-07-02 300         true        
 4 L             2018-07-02 180         false       
 5 C             2018-07-02 360         false       
 6 C             2018-07-02 120         false       
 7 G             2018-07-03 480         true        
 8 L             2018-07-03 30          true        
 9 L             2018-07-03 180         true        
10 L             2018-07-02 30          true        ")
x$taskDate <- as.Date(x$taskDate)

library(dplyr)
# library(tibble)
taskRates <- tibble::tribble(
  ~taskStaffName, ~before, ~after
 ,"J"           ,      22,     40
 ,"L"           ,      20,     20
 ,"G"           ,      20,     41
 ,"C"           ,      40,     41
)

cutoffDate <- as.Date("2018-11-18")
x %>%
  left_join(taskRates, by = "taskStaffName") %>%
  mutate(Budget = if_else(taskDate <= cutoffDate, before, after)) %>%
  select(-before, -after)
#    taskStaffName   taskDate taskMinutes taskBillable Budget
# 1              G 2018-07-02         300         true     20
# 2              G 2018-07-02         180         true     20
# 3              L 2018-07-02         300         true     20
# 4              L 2018-07-02         180        false     20
# 5              C 2018-07-02         360        false     40
# 6              C 2018-07-02         120        false     40
# 7              G 2018-07-03         480         true     20
# 8              L 2018-07-03          30         true     20
# 9              L 2018-07-03         180         true     20
# 10             L 2018-07-02          30         true     20

这很难假设一个有趣的截止日期。如果您计划使用多个月度的时间表费率,则应该重新考虑该问题,因为使用条件连接(例如范围连接,模糊连接)可能会更好。参考:


修改

如果您的逻辑永远不会比1个用户更改日期更复杂,那么您可以使用case_when逻辑,如@Gregor先前建议的那样:

x %>%
  mutate(
    Budget = case_when(
      taskStaffName == "L"     ~ 20,
      taskStaffName == "J" &
        taskDate <= cutoffDate ~ 20,
      TRUE                     ~ 40
    )
  )
#    taskStaffName   taskDate taskMinutes taskBillable Budget
# 1              G 2018-07-02         300         true     40
# 2              G 2018-07-02         180         true     40
# 3              L 2018-07-02         300         true     20
# 4              L 2018-07-02         180        false     20
# 5              C 2018-07-02         360        false     40
# 6              C 2018-07-02         120        false     40
# 7              G 2018-07-03         480         true     40
# 8              L 2018-07-03          30         true     20
# 9              L 2018-07-03         180         true     20
# 10             L 2018-07-02          30         true     20

答案 1 :(得分:0)

正如Gregor所说,我只是在解决此问题时才使用case_when。

我使用以下代码结束它:

MATCH
  (player:Player)-[:WEARS]->(armor:Armor)-[:PART_OF]->(dragonSet:ArmorSet),
  (missing)-[:PART_OF]->(dragonSet)
  WHERE NOT (player)-[:WEARS]->(missing:Armor)
WITH DISTINCT missing
MATCH (npc:Npc)-[:PROVIDES]->(quest:Quest)-[:REWARDS]->(missing)
  WHERE NOT (:Monster)-[:RANDOM_DROPS]->(missing)
RETURN npc.name AS npcName, quest.name AS questName, missing.name AS missingArmorName;

感谢大家抽出时间来尝试解决我的问题!

相关问题