0

I'm trying to make a crosstab with subtotals like the image below: enter image description here

I can create a general table using summarise with the following code:

 ds %>%
    group_by(Division,Category) %>% 
      summarise(Total = sum(Headcount), 
                Female = sum(Headcount[Gender == "Female"]),
                FemaleP = sum(Headcount[Gender == "Female"])/total,
                Hispanic = sum(Headcount[Race == "Hispanic/Latino"]),
                HispanicP = sum(Headcount[Race == "Hispanic/Latino"])/total,
)

But I'm struggling to get subtotals for the division along with subtotals in the blue highlighted row in the screenshot. Any help would be greatly appreciated! Thanks so much.

gbis
  • 43
  • 4
  • Loos similar to this one https://stackoverflow.com/questions/4104406/calculating-subtotals-in-r . – Denny Chen Apr 11 '22 at 16:50
  • From what I understand the post linked shows how to calculate a total separately using the aggregate function but how do I splice it into my table. In particular the blue highlighted subtotal seems difficult to create. – gbis Apr 11 '22 at 17:00
  • 1
    The janitor package has some amazing crosstabulation abilities so a strategy of filtering for what you want and then splicing together the various outputs should be relatively easy. – mkpt_uk Apr 11 '22 at 18:21
  • I haven't found a way to have multilevel crosstabs using the janitor package, although it gets me close. Like I can't have it break the table out by Division and Rank on the row level. – gbis Apr 11 '22 at 19:45

0 Answers0