[英]Filter to remove all rows before the first time a particular value in a specific column appears
I would like to filter to remove all rows before the first time a particular value in a specific column appears.我想在特定列中的特定值第一次出现之前过滤以删除所有行。 For example, in the data frame below, I would like to remove all rows before bob
appears in column a
for the first time.例如,在下面的数据框中,我想在bob
第一次出现在a
列之前删除所有行。 Please note that the value of bob
repeats a second time -I only want to remove the rows before the first time bob
appears.请注意bob
的值第二次重复 - 我只想在第一次bob
出现之前删除行。
(dat<-data.frame(a= c("pete", "mike", "bob", "bart", "bob"), b=c(1,2,3,4,5), c=c("home", "away", "home", "away", "gone")))
a b c
1 pete 1 home
2 mike 2 away
3 bob 3 home
4 bart 4 away
5 bob 5 gone
I want the resulting data frame to look like the following:我希望生成的数据框如下所示:
a b c
1 bob 3 home
2 bart 4 away
3 bob 5 gone
dplyr
way using slice
. dplyr
方式使用slice
。
library(dplyr)
dat %>% slice(which.max(a == "bob") : n())
# a b c
#1 bob 3 home
#2 bart 4 away
#3 bob 5 gone
which in base R would be这在基础 R 中将是
dat[which.max(dat$a == "bob") : nrow(dat), ]
cumsum
is usually a good candidate for such tasks cumsum
通常是此类任务的理想人选
dat[cumsum(dat$a == "bob") >= 1, ]
# a b c
#3 bob 3 home
#4 bart 4 away
#5 bob 5 gone
We can use cummax
我们可以使用cummax
library(dplyr)
dat %>%
filter(cummax(a == "bob") > 0)
# a b c
#1 bob 3 home
#2 bart 4 away
#3 bob 5 gone
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.