what if any parts would be parametrised ?
It does look somewhat odd to mix dplyr mutates, but favour aggregate over dplyr group_by/summarise, so I'd probably start there.
final note, pasting an image of a data.frame while giving 'some idea' of what you are working with, doesnt do much to help forum users to help you, rather it would seem to demand of them to do more work to get your existing code up and running. Are they expected to type out the contents of the data.frame ?
There are tools for communicating data.frames (or samples thereof) to the forum. base::dput() and datapasta package functions for example.
Here is the guide.