我有一个包含两列的数据帧,如下所示.
nct_id recrstdt check start_date sdt
NCT02277743 2014-12-21 2 2014-10-31 16425
NCT02277769 2014-12-21 2 2014-11-30 16425
NCT03131648 2017-06-08 2 2017-05-30 17325
NCT03160885 2017-07-14 2 2017-06-12 17361
NCT03349060 2018-01-17 2 2017-12-07 17548
NCT03569293 2018-08-21 2 2018-08-13 17764
NCT03575871 2018-07-12 2 2018-06-29 17724
NCT03607422 2018-08-09 2 2018-07-27 17752
NCT04146363 <NA> 2 2019-09-24 Inf
NCT04162769 <NA> 2 2019-10-04 Inf
NCT04178967 <NA> 2 2019-10-29 Inf
structure(list(nct_id = c("NCT02277743", "NCT02277769", "NCT03131648",
"NCT03160885", "NCT03349060", "NCT03569293", "NCT03575871", "NCT03607422",
"NCT04146363", "NCT04162769", "NCT04178967"), recrstdt = structure(c(16425,
16425, 17325, 17361, 17548, 17764, 17724, 17752, Inf, Inf, Inf
), class = "Date"), check = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
), start_date = structure(c(16374, 16404, 17316, 17329, 17507,
17756, 17711, 17739, 18163, 18173, 18198), class = "Date"), sdt = c(16425,
16425, 17325, 17361, 17548, 17764, 17724, 17752, Inf, Inf, Inf
)), row.names = c(NA, -11L), class = "data.frame")
我使用以下代码创建列sdt,该列按顺序从recstdt或start_date中 Select 第一个非missing.但是,如果Start_date为NA,则不会 Select start_date.
Recrstdt的类别为"Date"
mutate(check=ifelse(is.na(recrstdt), 1, 2),
sdt=ifelse(!is.na(recrstdt), recrstdt, start_date))
任何帮助弄清楚这一点的人都非常感激.