A basic indexing query in R

You can use forcats::fct_lump() for this task

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.