Working with factors on R 4.5.1

Hallo,

I am trying to mutate a factor on R ver 4.5.1 and things seems not to work out. Yes my code is running perfectly well but not producing what I expect.
I 'd like to lump less frequent factors together into "other" category as follows.

df |>
mutate(
important_benefit  =  fct_lump(important_benefit, n = 10)
)

The output I get does not lamp the last three infrequent factor levels in to "other" category as expected (despite not giving me any warning) though it shows it has changed it from character variable to factor.
Could there be something wrong with this version of R or my code?

This problem only occurs when I am mutating after I have a group summary however when I mutate before grouping, everything works well

I wonder if you might need to ungroup before doing this. It would help us more if you gave the full workflow and some example data.

I don't see anything wrong in the bit of code you've posted. This works, for example:

library(forcats)
library(dplyr)

set.seed(6181)
probs <- rbeta(26, shape1 = 1, shape2 = 5)
probs <- probs / sum(probs) 


df <- data.frame(
  important_benefit = sample(letters, size = 1000, replace = TRUE, prob = probs)
)

df <- df |> 
  mutate(
    important_benefit = factor(important_benefit)
  )

df <- df |> 
  mutate(
    important_benefit  =  fct_lump(important_benefit, n = 10)
  )

head(df, 10)

gives

> head(df, 10)
   important_benefit
1              Other
2                  n
3                  q
4                  c
5                  q
6              Other
7              Other
8              Other
9                  q
10                 e

If you mean after group_by(...) |> summarise(...) and you grouped by multiple variables, your resulting data frame could still be grouped and you may be lumping each group individually.