简体   繁体   English

过滤以在特定列中的特定值第一次出现之前删除所有行

[英]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.

相关问题 过滤以删除特定列中特定值之前的所有行,而该特定值出现多次 - Filter to remove all rows before a particular value in a specific column, while this particular value occurs several time 如何在特定列的行中向特定 position 添加特定值? - How to add a particular value to a particular position in rows of a specific column? 第一次看到特定观察结果后如何删除行 - How to remove rows after a particular observation is seen for the first time 如果另一列上出现零值,则删除带有因子的行 - Remove rows with a factor if zero value appears on another column 在使用 dplyr 的特定列值的第一个实例之后过滤 dataframe 中的 R 中的行 - Filter rows in dataframe in R after first instance of specific column value using dplyr 如何删除基于R中特定列的所有行? - How to remove all rows based on a particular column in R? 根据列的第一个值对所有行进行子集 - Subset All Rows Based on First Value of Column 如何使用 dplyr 过滤特定列中值为 1 且所有 rest 为 0 的行? - How to use dplyr to filter rows where value in a specific column is 1 and all the rest are 0? 如果行有,则在特定值的第一个值处过滤行 - Filter rows at the first value of specific value if the row have it 删除在特定时间戳之前具有值的行 - Remove rows which have values before a specific time stamp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM