0

I'm trying to isolate the rows of the last 9 instances a certain value occurrs. However, some values occur less than 9 times. Is it possible to ignore them and only slice those rows that fulfill the condition (which is: value occurs at least 9 times in the vector)?

When I run the piece of code below, I get the following error:

slice() expressions should return either all positive or all negative.

gtr<-rdat2%>%
    group_by(HomeTeam)%>%
    arrange(rdat2$HsT,HomeTeam)%>%
    slice((n()-8):n())

I was thinking of making an if-statement but don't know how to capture that the value must occur a certain number of times. Any help would be greatly appreciated.

Nckh
  • 67
  • 1
  • 6

1 Answers1

1

You could try the following :

library(dplyr)
gtr <- rdat2 %>%
         group_by(HomeTeam)%>%
         arrange(HsT,HomeTeam)%>%
         slice(max(n()-8, 1):n())

max(n()-8, 1) will select n() - 8 or 1 whichever is higher so that it will help avoid the negative subscript error and also avoid the need of if/else statement.

If you are on dplyr > 1.0.0 you can also use slice_tail which makes slicing simpler.

gtr <- rdat2 %>%
        arrange(HsT,HomeTeam)%>%
        group_by(HomeTeam)%>%
        slice_tail(n = 9)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks for your quick response. But that way, I'd still have to keep one row for each value occuring less than 9 times. How would I remove that? – Nckh Oct 04 '20 at 14:27
  • This keeps last 9 rows in each group or all the rows whichever is smaller. If that is not what you want I probably misunderstood your question. Can you provide an example and show expected output for it? – Ronak Shah Oct 04 '20 at 14:33
  • For my present case your example works for me. Initially I wanted to slice the last 9 rows in each group or otherwise drop the group completely. I'll provide an example in a bit but for now this seems to work fine for me. Thanks. – Nckh Oct 04 '20 at 14:44