我有一个包含两列的数据帧,如下所示.

    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))
                   

任何帮助弄清楚这一点的人都非常感激.

推荐答案

is.finite(NA) == FALSE开始,我们既可以考虑Inf也可以考虑NA.如果两者都是InfNA,则给Inf.

> dat |>
+   transform(check=replace(check, !is.finite(recrstdt), 1),
+             sdt=as.Date(mapply(min, recrstdt, start_date, na.rm=TRUE)))
        nct_id   recrstdt check start_date        sdt
1  NCT02277743 2014-12-21     2 2014-10-31 2014-10-31
2  NCT02277769 2014-12-21     2 2014-11-30 2014-11-30
3  NCT03131648 2017-06-08     2 2017-05-30 2017-05-30
4  NCT03160885 2017-07-14     2 2017-06-12 2017-06-12
5  NCT03349060 2018-01-17     2 2017-12-07 2017-12-07
6  NCT03569293 2018-08-21     2 2018-08-13 2018-08-13
7  NCT03575871 2018-07-12     2 2018-06-29 2018-06-29
8  NCT03607422 2018-08-09     2 2018-07-27 2018-07-27
9  NCT04146363        Inf     1 2019-09-24 2019-09-24
10 NCT04162769        Inf     1        Inf        Inf
11 NCT04178967       <NA>     1 2019-10-29 2019-10-29

Data:

> dput(dat)
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, NA
), 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, Inf, NA), class = "Date"), sdt = c(16425, 
16425, 17325, 17361, 17548, 17764, 17724, 17752, Inf, Inf, Inf
)), row.names = c(NA, -11L), class = "data.frame")

R相关问答推荐

使用geom_rect的带有事件注释的时间序列图

想要创建一个可以存储四个多元正态总体的多元偏度的载体

返回句子中最长的偶数长单词

使用gsim删除特殊词

使用case_when和Mutate搜索多个列以寻找条件

如何使用Cicerone指南了解R Shiny中传单 map 的元素?

通过Plotly绘制线串几何形状的3D图

提取rame中对应r中某个变量的n个最小正值和n个最大负值的条目

如何在RMarkdown LaTex PDF输出中包含英语和阿拉伯语?

如何计算R数据集中每个女性的子元素数量?

获取列中值更改的行号

整数成随机顺序与约束R?

在数学中正确显示摄氏度、开氏度或华氏度

如何调整曲线图中的y轴标签?

如何从像glm这样的模型中提取系数表的相关性?

以更少间隔的较小表中的聚合离散频率表

如何在科学记数法中显示因子

Data.table';S GForce-将多个函数应用于多列(带可选参数)

如何更改包中函数中的参数?

GOGPLATE geom_boxploy色彩疯狂