Further trials to get the function running with dplyr. However, the grouping does not seem to work properly.