dimanche 4 octobre 2020

Conditional Slicing of Rows in R

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.

Aucun commentaire:

Enregistrer un commentaire