如果Date为max(date),则创建具有diff和Principal总和的新列

数据:

  ID           Par      Date      Principal  summ    diff  summ.new
007036JH0    7707896 2020-03-25   135626.12 7707896 -0.01 135626.12
007036JH0    7707896 2020-04-27   153919.34 7707896 -0.01 153919.34
007036JH0    7707896 2020-05-26   189843.35 7707896 -0.01 189843.35
007036JH0    7707896 2020-06-25   184767.42 7707896 -0.01 184767.42
007036JH0    7707896 2020-07-27   187981.41 7707896 -0.01 187981.41
007036JH0    7707896 2020-08-25   188668.81 7707896 -0.01 188668.81
007036JH0    7707896 2020-09-25   185205.98 7707896 -0.01 185205.98
007036JH0    7707896 2020-10-26   167851.27 7707896 -0.01 167851.27

查询:

使用提到的代码和数据框,如果Date为max(date),我尝试将diff添加到Principal,否则只需将Principal分配给新列summ.new。

注意:这是示例数据,因此只有一个ID。我有几个ID,因此我正在使用for循环。 让我知道是否需要更多信息。

码:

for(i in vec){   
  if(df5$Post.Date[j]==max(df5$Post.Date)){     
     df5$summ.new <- df5$Principal.x + abs(df5$diff)
  } else {
     df5$summ.new <- df5$Principal.x    
  }}
评论
  • 雪
    回复

    We can add diff value if Date is max Date for each ID.

    library(dplyr)
    
    df %>%
      group_by(ID) %>%
      mutate(summ.new = ifelse(Date == max(Date), Principal + abs(diff), Principal)) 
    

    The same logic in data.table can be written as :

    library(data.table)
    setDT(df)[, summ.new := ifelse(Date == max(Date), Principal + abs(diff), Principal), ID]
    

    Although using ifelse is easy to understand but this can be done without it as well.

    df %>%
      group_by(ID) %>%
      mutate(summ.new = Principal + (abs(diff) * (Date == max(Date))))
    

    setDT(df)[, summ.new := Principal + (abs(diff) * (Date == max(Date))), ID]