Thank you very much for your answer this is really helfull. Now my only problem is that I cannot use the aggregate_key function on my dataset. I get the following error:
Error in UseMethod("aggregate_key") :
no applicable method for 'aggregate_key' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"
My dataset is similar to the one in the example.