How to group by category when creating XMR columns?

In my example df is just sample data for the purpose of making a REPRoducible EXample (reprex), simply replace df by somedata in the code.