FYI, cross-posting is fine questions on this site, we just ask that you link to the question on other sites. See the FAQ on cross-posting.
Here's the link to the same question on Stack Overflow: dataframe - Summarize data at different aggregate levels - R and tidyverse - Stack Overflow.