user5424264
user5424264

Reputation: 105

Remove rows after first occurrence of a certain value

I have the following df:

df <- data.frame(var1 = c(1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9),
                 var2 = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"))

I want to filter after the moment the first 5 (var1) is reached. So the result should be:

  var1 var2
1    1    a
2    2    b
3    2    c
4    3    d
5    4    e
6    5    f

I tried:

   df1 <- df %>%
     mutate(a = !duplicated(var1)) %>%
     filter(var1 < 6 & a == TRUE)

But the problem here is that is also deletes the duplicates the var2(c).

Upvotes: 3

Views: 1064

Answers (1)

akrun
akrun

Reputation: 886978

We can use cumsum to create a logical vector

df %>% 
  filter(cumsum(var1 == 5) < 2)
#    var1 var2
#1    1    a
#2    2    b
#3    2    c
#4    3    d
#5    4    e
#6    5    f

Or another option is slice with match

df %>%
   slice(seq(match(5, var1)))

Or with which.max (assuming there is a 5 value)

df %>%
   slice(seq(which.max(var1 == 5)))

Upvotes: 5

Related Questions