这是我的数据,我希望在事件后删除ID的所有数据
ID Event time
1 0 1
1 1 2
2 0 3
1 0 4
2 0 5
因为对于ID号1,事件大于0,我想删除ID 1的所有下一个数据。所以,我删除了第4行,我的理想输出将是
ID Event time
1 0 1
1 1 2
2 0 3
2 0 5
我该怎么做?
dput(df)
structure(list(ID = c(1L, 1L, 2L, 1L, 2L), Event = c(0L, 1L,
0L, 0L, 0L), time = 1:5), .Names = c("ID", "Event", "time"), class = "data.frame", row.names = c(NA,
-5L))
答案 0 :(得分:4)
使用dplyr,filter
time
的{{1}}值小于Event
为1的最小值ID
:
library(dplyr)
df %>% group_by(ID) %>% filter(time <= min(time[Event == 1]))
## Source: local data frame [4 x 3]
## Groups: ID [2]
##
## ID Event time
## <int> <int> <int>
## 1 1 0 1
## 2 1 1 2
## 3 2 0 3
## 4 2 0 5
您可以将time
或row_number
与seq
一起使用,而不是使用which
。在基数R中,您可以使用ave
来处理分组,但它只能处理一个输入向量,因此seq
方法比使用time
更简单:
df[as.logical(ave(df$Event, df$ID, FUN = function(x) {
seq_along(x) <= min(which(x == 1))
})), ]
## ID Event time
## 1 1 0 1
## 2 1 1 2
## 3 2 0 3
## 5 2 0 5
这两种方法都取决于min(integer(0))
在Inf
没有1值时返回ID
这一事实,但添加if
条件以明确说明情况,如果你愿意的话。
答案 1 :(得分:1)
以下是match
使用data.table
library(data.table)
setDT(df)[, .SD[seq_len(match(1, Event, nomatch = .N))], ID]
# ID Event time
#1: 1 0 1
#2: 1 1 2
#3: 2 0 3
#4: 2 0 5