3

I'm attempting to remove all rows that come after a value (or values) but am running into some trouble.

I want to do the opposite of this: Filter to remove all rows before the first time a particular value in a specific column appears

Using the example dataframe from the above question:

(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 my result to look like this:

     a b    c
1 pete 1 home
2 mike 2 away
3  bob 3 home

Here is what I've tried so far:

dat %>% slice(which.min(a == "bob") : n())

But unlike which.max which removed everything before bob this doesn't remove anything after it.

Loons22
  • 371
  • 1
  • 7

2 Answers2

3

We can use

library(dplyr)
dat %>% 
     slice(seq(which.max(a == 'bob')))

Or with cumsum

dat %>% 
    filter(lag(cumsum(a == 'bob'), default = 0) < 1)

Or in base R

dat[seq_len(match('bob', dat$a)),]
akrun
  • 874,273
  • 37
  • 540
  • 662
3

Using row_number() :

library(dplyr)
dat %>% filter(row_number() <= match('bob', a))

#     a b    c
#1 pete 1 home
#2 mike 2 away
#3  bob 3 home
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213