Use inject for summation.
Replace 'if if if, ...' to case-when in get_group