How to rename factors in dplyr pipeline

I asked about method to rename factors and the selected answer uses fct_relabel on the factor vector only.

df$grp = df$grp %>% fct_relabel(str_replace, "_something", "")

But df in my usage is often the result from tidyr::gather and I don't feel like breaking the %>% pipe to name it just to do fct_relabel, and then resume the pipe. Any suggestions? Thanks!

How about this?
df <- df %>% mutate(grp = fct_relabel(grp, str_replace, "_something", "")) %>% ...

1 Like

Thanks. So simple! I should have looked up how to input the argument from the help :-

fct_relabel(.f, .fun, ...)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.