我有以下表格:
表1:
<表类>
ID
拒绝
新
过期
tbody><<tr>001 2021-02-21 2022-03-20 2021-05-20 001 2021-02-21 2022-03-20 2021-05-20 002 2021-06-21 NA 2021-06-20 002 2021-06-21 NA 2021-06-20 003 2021-05-20 NA 2021-05-20 003 2021-05-20 NA 2021-05-20 004 2021-05-20 2021-11-03 2022-06-20 004 2021-05-20 2021-11-03 2022-06-20 005 2021-05-20 2021-11-03 2022-06-20 005 2021-05-20 2021-11-03 2022-06-20 表类>
您可以使用left_join
然后coalesce
这两列
library(tidyverse)
left_join(df1, df2) %>%
mutate(New = pmin(Rejected, coalesce(New, date), Expired), date = NULL)
ID Rejected New Expired
1 1 2021-02-21 2021-02-21 2021-05-20
2 1 2021-02-21 2021-02-21 2021-05-20
3 2 2021-03-21 2021-03-21 2021-05-20
4 2 2021-03-21 2021-03-21 2021-05-20
5 3 2021-05-20 2021-04-30 2021-05-20
6 3 2021-05-20 2021-04-30 2021-05-20
7 4 2021-05-20 2021-05-20 2022-06-20
8 4 2021-05-20 2021-05-20 2022-06-20
9 5 2021-05-20 2021-05-20 2022-06-20
10 5 2021-05-20 2021-05-20 2022-06-20