根据日期和初始事件确定重复ID的后续事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据日期和初始事件确定重复ID的后续事件相关的知识,希望对你有一定的参考价值。
我正在尝试根据日期和初始事件确定重复ID。下面是一个示例数据集
+----+------------+-------------------------+
| ID | Date | Investigation or Intake |
+----+------------+-------------------------+
| 1 | 1/1/2019 | Investigation |
| 2 | 1/2/2019 | Investigation |
| 3 | 1/3/2019 | Investigation |
| 4 | 1/4/2019 | Investigation |
| 1 | 1/2/2019 | Intake |
| 2 | 12/31/2018 | Intake |
| 3 | 1/5/2019 | Intake |
+----+------------+-------------------------+
我想写R代码来通过ID从1到4(具有调查的ID)并查看他们是否有后续摄入量(摄入量发生在比调查日期更晚的日期)。所以预期的输出看起来像这样:
+----+------------+-------------------------+------------+
| ID | Date | Investigation or Intake | New Column |
+----+------------+-------------------------+------------+
| 1 | 1/1/2019 | Investigation | Sub Intake |
| 2 | 1/2/2019 | Investigation | None |
| 3 | 1/3/2019 | Investigation | Sub Intake |
| 4 | 1/4/2019 | Investigation | None |
| 1 | 1/2/2019 | Intake | |
| 2 | 12/31/2018 | Intake | |
| 3 | 1/5/2019 | Intake | |
+----+------------+-------------------------+------------+
代码将是什么样的解决方案?我猜这将是一些循环功能?
谢谢!
答案
您可以使用dplyr
包执行此操作,并使用一些ifelse
语句根据需要创建新列。而不是使用循环而只是使用lead
函数检查组中的下一个条目。此解决方案假设在每个组中您将有一个“调查”,然后是0或更多“后续”的“摄入”条目。
library(dplyr)
df <- data.frame(ID = c(1, 2, 3, 4, 1, 2, 3),
Date = as.Date(c("2019-01-01", "2019-01-02", "2019-1-03", "2019-01-04", "2019-01-02", "2018-12-31", "2019-1-5")),
Investigation_or_Intake = c("Investigation", "Investigation", "Investigation", "Investigation", "Intake", "Intake", "Intake"),
stringsAsFactors = FALSE)
df %>%
group_by(ID) %>% # Make groups according to ID column
mutate(newcol = ifelse(lead(Date) > Date, "Sub Intake", "None"), # Check next entry in the group to see if Date is after current
newcol = ifelse(Investigation_or_Intake == "Investigation" & is.na(newcol), "None", newcol)) # Change "Investigation" entries with no Intake to "None"
这给了
ID Date Investigation_or_Intake newcol
<dbl> <date> <chr> <chr>
1 1 2019-01-01 Investigation Sub Intake
2 2 2019-01-02 Investigation None
3 3 2019-01-03 Investigation Sub Intake
4 4 2019-01-04 Investigation None
5 1 2019-01-02 Intake NA
6 2 2018-12-31 Intake NA
7 3 2019-01-05 Intake NA
以上是关于根据日期和初始事件确定重复ID的后续事件的主要内容,如果未能解决你的问题,请参考以下文章
Jquery:如何根据属性值将按键事件绑定到输入类型“数字”[重复]