dplyr
和
ifelse
dat <- structure(list(GenIndID = c("BHS_034", "BHS_034", "BHS_068",
"BHS_068", "BHS_068", "BHS_068", "BHS_068", "BHS_068", "BHS_068",
"BHS_068", "BHS_068"), IndID = c("BHS_034_A", "BHS_034_A", "BHS_068_A",
"BHS_068_A", "BHS_068_A", "BHS_068_A", "BHS_068_A", "BHS_068_A",
"BHS_068_A", "BHS_068_A", "BHS_068_A"), Fate = c("Mort", "Mort",
"Alive", "Alive", "Alive", "Alive", "Alive", "Alive", "Alive",
"Alive", "Alive"), Status = c("Alive", "Mort", "Alive", "Alive",
"MIA", "Alive", "MIA", "Alive", "MIA", "Alive", "Alive"), Type = c("Linked",
"Linked", "SOB", "SOB", "SOB", "SOB", "SOB", "SOB", "SOB", "SOB",
"SOB"), SurveyID = c("GYA13-1", "GYA14-1", "GYA13-1", "GYA14-1",
"GYA14-2", "GYA15-1", "GYA16-1", "GYA16-2", "GYA17-1", "GYA17-3",
"GYA15-2"), SurveyDt = structure(c(1379570400, 1407477600, 1379570400,
1407477600, 1409896800, NA, 1462946400, 1474351200, 1495519200,
1507010400, 1441951200), tzone = "", class = c("POSIXct", "POSIXt"
))), row.names = c(NA, 11L), .Names = c("GenIndID", "IndID",
"Fate", "Status", "Type", "SurveyID", "SurveyDt"), class = "data.frame")
> dat
GenIndID IndID Fate Status Type SurveyID SurveyDt
1 BHS_034 BHS_034_A Mort Alive Linked GYA13-1 2013-09-19
2 BHS_034 BHS_034_A Mort Mort Linked GYA14-1 2014-08-08
3 BHS_068 BHS_068_A Alive Alive SOB GYA13-1 2013-09-19
4 BHS_068 BHS_068_A Alive Alive SOB GYA14-1 2014-08-08
5 BHS_068 BHS_068_A Alive MIA SOB GYA14-2 2014-09-05
6 BHS_068 BHS_068_A Alive Alive SOB GYA15-1 <NA>
7 BHS_068 BHS_068_A Alive MIA SOB GYA16-1 2016-05-11
8 BHS_068 BHS_068_A Alive Alive SOB GYA16-2 2016-09-20
9 BHS_068 BHS_068_A Alive MIA SOB GYA17-1 2017-05-23
10 BHS_068 BHS_068_A Alive Alive SOB GYA17-3 2017-10-03
11 BHS_068 BHS_068_A Alive Alive SOB GYA15-2 2015-09-11
更具体地说,分组依据
GenIndID
我想创建一个最大值的新日期字段
SurveyDt
基于两个条件
Type
和
Fate
. 此外,我希望最大日期仅评估
测量数据
Status == Alive
. 我下面的代码生成了所有
NA
值,而不是描述的日期字段
BHS_068
我最近看到
case_when
这在这里可能是合适的,但我不能正确地实现它。
dat %>% group_by(GenIndID) %>%
mutate(NewDat = as.POSIXct(ifelse(Type == "SOB" & Fate == "Alive", max(SurveyDt[Status == "Alive"], na.rm = F), NA),
origin='1970-01-01', na.rm=T)) %>%
as.data.frame()