按条件过滤数据帧,包括该条件之后的数据

Filtering data frame by condition including data after that condition

是否有一种简单的方法来过滤我的数据框,以便过滤掉符合某些条件的行之后的所有行(包括符合某些条件的行)?这里的问题是我希望它足够健壮以处理不满足该条件的情况,在这种情况下将返回整个数据帧。如果听起来令人困惑,请查看下面的示例:

library(dplyr)

## Works
mtcars %>% 
  as_tibble() %>% 
  filter(between(row_number(), 1, which(mpg == 17.8)))

#> # A tibble: 11 x 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> 11  17.8     6  168.   123  3.92  3.44  18.9     1     0     4     4

## Doesn't work
mtcars %>% 
  as_tibble() %>% 
  filter(between(row_number(), 1, which(mpg == 30.5)))

#> Error in filter_impl(.data, quo): Evaluation error: Expecting a single value: [extent=0]..

reprex package (v0.2.0) 创建于 2018-08-12。

我认为您的具体示例不起作用,因为没有等于 30.5 的 mpg,但是,您会得到与 mpg 等于 21.0 相同的错误,因为有两行具有该值。您需要选择是想要该条件的第一个还是最后一个实例

library(tidyverse)

#max row
mtcars %>% 
  as_tibble() %>% 
  filter(between(row_number(), 1, which(mtcars$mpg == 21.0)[length(which(mtcars$mpg == 21.0))]))
#> # A tibble: 2 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1    21     6   160   110   3.9  2.62  16.5     0     1     4     4
#> 2    21     6   160   110   3.9  2.88  17.0     0     1     4     4

#min row
mtcars %>% 
  as_tibble() %>% 
  filter(between(row_number(), 1, which(mtcars$mpg == 21.0)[1]))
#> # A tibble: 1 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1    21     6   160   110   3.9  2.62  16.5     0     1     4     4

我选择的示例恰好是第 1 行和第 2 行,但它说明了这个想法。

编辑

Lamia 的另一个回答要优雅得多,我可能想得太多了,但我觉得我需要想出点什么

library(dplyr)


filter_if_condition <- function(.data, condition, yes){
  test_cond <- enquo(condition)
  yes_filter <- enquo(yes)

  if(.data %>% filter(!!test_cond) %>% nrow() > 0){
    .data %>% filter(!!yes_filter)
  }
  else{.data}
}

mtcars %>% 
  as_tibble() %>% 
  filter_if_condition(366.0 %in% mpg, between(row_number(), 1, which(mpg == 366)[1]))
#> # A tibble: 32 x 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>  * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ... with 22 more rows

mtcars %>% 
  as_tibble() %>% 
  filter_if_condition(18.1 %in% mpg, between(row_number(), 1, which(mpg == 18.1)[1]))
#> # A tibble: 6 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  21       6   160   110  3.9   2.62  16.5     0     1     4     4
#> 2  21       6   160   110  3.9   2.88  17.0     0     1     4     4
#> 3  22.8     4   108    93  3.85  2.32  18.6     1     1     4     1
#> 4  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1
#> 5  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2
#> 6  18.1     6   225   105  2.76  3.46  20.2     1     0     3     1

您可以包含一个 ifelse 语句来检查该值是否存在于数据框中。此外,您需要 select 验证条件的第一行,以说明该值多次出现的情况(在您的示例中为 21.0)

library(dplyr)
mtcars %>% 
as_tibble() %>% 
filter(between(row_number(), 1,ifelse(!any(mpg == 30),n(),which(mpg == 30)[1]-1)))
## returns the whole tibble

mtcars %>% 
as_tibble() %>% 
filter(between(row_number(), 1,ifelse(!any(mpg == 21),n(),which(mpg == 21)[1]-1)))
## Returns a tibble with 0 rows

mtcars %>% 
as_tibble() %>% 
filter(between(row_number(), 1,ifelse(!any(mpg == 21.4),n(),which(mpg == 21.4)[1]-1)))
## returns:
# A tibble: 3 x 11
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1  21.0     6   160   110  3.90 2.620 16.46     0     1     4     4
2  21.0     6   160   110  3.90 2.875 17.02     0     1     4     4
3  22.8     4   108    93  3.85 2.320 18.61     1     1     4     1