Home > front end >  Filter to remove all rows before a particular value in a specific column, while this particular valu
Filter to remove all rows before a particular value in a specific column, while this particular valu

Time:12-07

I would like to filter to remove all rows before a particular value in a specific column. For example, in the data frame below, I would like to remove all rows before "1" that appears in column x, for as much as "1" occurs. Please note that the value of "1" repeats many times and I want to remove the "NA" rows before the "1" in column x, regarding column a. Thanks

a   b   x
1   1   NA
1   2   NA
1   3   1
1   4   0
1   5   0
1   6   NA
1   7   NA
2   1   NA
2   2   NA
2   3   1
2   4   NA
2   5   0
2   6   0
2   7   NA
3   1   NA
3   2   NA
3   3   NA
3   4   NA
3   5   1
3   6   0
3   7   NA

the desired output would be like this:

a   b   x
1   3   1
1   4   0
1   5   0
1   6   NA
1   7   NA
2   3   1
2   4   NA
2   5   0
2   6   0
2   7   NA
3   5   1
3   6   0
3   7   NA

CodePudding user response:

Does this solve your problem?

library(tidyverse)

dat <- read.table(text = "a   b   x
1   1   NA
1   2   NA
1   3   1
1   4   0
1   5   0
1   6   NA
1   7   NA
2   1   NA
2   2   NA
2   3   1
2   4   NA
2   5   0
2   6   0
2   7   NA
3   1   NA
3   2   NA
3   3   NA
3   4   NA
3   5   1
3   6   0
3   7   NA", header = TRUE)

dat %>%
  group_by(a) %>%
  filter(cummax(!is.na(x)) == 1)
#> # A tibble: 13 × 3
#> # Groups:   a [3]
#>        a     b     x
#>    <int> <int> <int>
#>  1     1     3     1
#>  2     1     4     0
#>  3     1     5     0
#>  4     1     6    NA
#>  5     1     7    NA
#>  6     2     3     1
#>  7     2     4    NA
#>  8     2     5     0
#>  9     2     6     0
#> 10     2     7    NA
#> 11     3     5     1
#> 12     3     6     0
#> 13     3     7    NA

Created on 2021-12-07 by the reprex package (v2.0.1)

  • Related