Probably something with group_by but hard to tell what exactly without an example.
group_by
Can you provide a reproducible example?