is there a way of keeping all obs condtion on top_n value from another column

i need to find a way using tidyverse to keep all obs. but filtering all other observation than the top_n values from a column.

example:

Name| title | rank

John | xxxxxx | 1

John| yyyyyyy | 1

John| zzzzzz | 1

max | xxxxxx | 3

max | yyyyyyy | 3

max | zzzzzz | 3

evan| xxxxxx | 2

evan| yyyyyyy | 2

evan| zzzzzz | 2

then i would like to keep top_n rank = 2, and still keep all observation of that rank so not only unqiue rows

so it would look like this Name| title | value

John | xxxxxx | 100

John| yyyyyyy | 100

John| zzzzzz | 100

evan| xxxxxx | 200

evan| yyyyyyy | 200

evan| zzzzzz | 200

is this possible in dplyr / with tidyverse?



Read more here: https://stackoverflow.com/questions/67213811/is-there-a-way-of-keeping-all-obs-condtion-on-top-n-value-from-another-column

Content Attribution

This content was originally published by Cafi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: