【发布时间】:2016-06-26 04:54:07
【问题描述】:
我有一个如下的数据框
structure(list(HospNum_Id = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L), VisitDate = c("13/02/03", "13/04/05", "13/05/12",
"13/12/06", "13/04/12", "13/05/13", "13/06/14", "13/04/15", "03/04/15",
"04/05/16", "04/06/16"), EVENT = c("EMR", "RFA", "nothing", "nothing",
"EMR", "nothing", "EMR", "EMR", "RFA", "EMR", "nothing")), .Names = c("HospNum_Id",
"VisitDate", "EVENT"), class = "data.frame", row.names = c(NA,
-11L))
我只想选择当前行EVENT 为“EMR"”的行,并且对于每个HospNum_Id,此行之前的行(按日期升序排列)为“无”。
我想要的输出是:
HospNum_Id VisitDate EVENT
2 13/12/06 nothing
2 13/04/12 EMR
2 13/05/13 nothing
2 13/06/14 EMR
但我目前的输出是:
HospNum_Id VisitDate EVENT
(int) (chr) (chr)
1 2 13/04/12 EMR
2 2 13/06/14 EMR
3 2 13/04/15 EMR
目前我有以下代码,但我认为它让我失望了,因为我在过滤器中使用 first 而不是一个短语,意思是 "before the row that has EMR in the EVENT":
Upstaging<-Therap %>%
arrange(HospNum_Id, as.Date(Therap$VisitDate, '%d/%m/%y')) %>%
group_by(HospNum_Id) %>%
filter(first(EVENT == "nothing") & EVENT == "EMR")
【问题讨论】:
标签: r