Select R 中分组数据条件后的行

Select rows after condition in grouped data in R

我有以下数据:

z <- data.frame(

  id = c("a", "a", "b", "b", "b", "c", "c", "c", "c"), 
  x = c(10, 4, 3, 7, 10, 3, 2, 6, 2)

)

我需要按“id”分组,然后 select 所有行,包括满足以下条件 (x >=5 & x <=8)

理想情况下我的数据应该是这样的

id   x 
b    7
b    10
c    6
c    2  

我尝试了以下方法但没有成功。任何帮助表示赞赏。

z %>%
  group_by(id) %>%
  filter(row_number() >= min(which(x>= 5 & x<=8)))

我觉得cumany(累计any)就是你需要的。

基本上是这样

cumany(c(F,F,T,F,F,F))
# [1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE

关于您的数据:

library(dplyr)
z %>%
  group_by(id) %>%
  filter(cumany(between(x, 5, 8))) %>%
  ungroup()
# # A tibble: 4 x 2
#   id        x
#   <chr> <dbl>
# 1 b         7
# 2 b        10
# 3 c         6
# 4 c         2

你可以通过将其添加为变量来查看它的作用,仅用于演示:

z %>%
  group_by(id) %>%
  mutate(keep = cumany(between(x, 5, 8))) %>%
  ungroup()
# # A tibble: 9 x 3
#   id        x keep 
#   <chr> <dbl> <lgl>
# 1 a        10 FALSE
# 2 a         4 FALSE
# 3 b         3 FALSE
# 4 b         7 TRUE     # every 'keep' in id='b' after this will be TRUE
# 5 b        10 TRUE 
# 6 c         3 FALSE
# 7 c         2 FALSE
# 8 c         6 TRUE     # ditto, id='c'
# 9 c         2 TRUE