我有一个数据结构,采用data.table格式:
ID Cycle Cycle_Day Cycle_Date Positive_Test_Date
1 1 1 3/28/2019 NA
1 1 2 3/29/2019 NA
1 1 3 3/30/2019 NA
1 1 NA NA 3/29/2019
1 2 1 4/23/2019 NA
1 2 2 4/24/2019 NA
1 2 3 4/25/2019 NA
1 2 NA NA 4/25/2019
2 1 1 3/18/2019 NA
2 1 2 3/19/2019 NA
2 1 3 3/20/2019 NA
2 1 NA NA 3/18/2019
2 2 1 4/23/2019 NA
2 2 2 4/24/2019 NA
2 2 3 4/25/2019 NA
2 2 NA NA 4/24/2019
我希望创建一个名为“LH_Date”的新列,该列将在每个ID和每个周期中复制事件Cycle_Date和Positive_Test_Date匹配的日期。否则,该值为NA。应如下所示:
ID Cycle Cycle_Day Cycle_Date Positive_Test_Date LH_Date
1 1 1 3/28/2019 NA NA
1 1 2 3/29/2019 NA 3/29/2019
1 1 3 3/30/2019 NA NA
1 1 NA NA 3/29/2019 NA
1 2 1 4/23/2019 NA NA
1 2 2 4/24/2019 NA NA
1 2 3 4/25/2019 NA 4/25/2019
1 2 NA NA 4/25/2019 NA
2 1 1 3/18/2019 NA 3/18/2019
2 1 2 3/19/2019 NA NA
2 1 3 3/20/2019 NA NA
2 1 NA NA 3/18/2019 NA
2 2 1 4/23/2019 NA NA
2 2 2 4/24/2019 NA 4/24/2019
2 2 3 4/25/2019 NA NA
2 2 NA NA 4/24/2019 NA