查找r中跨行日期的最小值



我有以下表格:

表1:

<表类> ID 拒绝 新 过期 tbody><<tr>0012021-02-212022-03-202021-05-200012021-02-212022-03-202021-05-200022021-06-21NA2021-06-200022021-06-21NA2021-06-200032021-05-20NA2021-05-200032021-05-20NA2021-05-200042021-05-202021-11-032022-06-200042021-05-202021-11-032022-06-200052021-05-202021-11-032022-06-200052021-05-202021-11-032022-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

相关内容

  • 没有找到相关文章