Had to do this for thesis back in the day, feels a lot more plesant in a dplyr chain: https://gist.github.com/mine-cetinkaya-rundel/75164f0326ece3cd7f2e8ce52d827e60