Home > OS >  How to filter out all rows of data frame after the last value of 1 in the column Z?
How to filter out all rows of data frame after the last value of 1 in the column Z?

Time:06-02

I have the following data frame:

|  Y  |  Z  |
-----------------
  62     0
  65     0
  59     1
  66     0
  64     1
  64     1
  57     0
  68     1
  59     0
  60     0

How can I filter out the Z column so, that all the "leftover values" after the final occurance of the value 1 will be filtered out (in this case all the zeroes after the last 1)? In the case of the above example, the filtered data frame would become:

|  Y  |  Z  |
-----------------
  62     0
  65     0
  59     1
  66     0
  64     1
  64     1
  57     0
  68     1

Also, how could I do the filtering for all the values before the first 1 (filter out all the values which precede it) ..?

CodePudding user response:

You can delete all rows after the last occurrence of a value like this:

library(dplyr)

df %>% 
  slice(1:max(which(Z == 1)))

Output:

   Y Z
1 62 0
2 65 0
3 59 1
4 66 0
5 64 1
6 64 1
7 57 0
8 68 1

CodePudding user response:

Another possible solution:

library(dplyr)

df %>%
  filter(!(Z == 0 & data.table::rleid(Z) %>% "%in%"(c(1, max(.)))))

#>    Y Z
#> 1 59 1
#> 2 66 0
#> 3 64 1
#> 4 64 1
#> 5 57 0
#> 6 68 1
  • Related