Package for pivot table


I have more than two categorical variables. I want two build pivot table that will have subgroups. which package is recommend to use?
On the picture there is some prototype of table I want to build


It would be useful to provide a reproducible example of your data:
FAQ: What's a reproducible example (reprex) and how do I create one?

Learning the tidyverse is a good jumping off point for analysis in R; see R4DS.

To do something like you're asking, you'll likely want to use a dplyr pipeline involving group_by() and summarise(), e.g., using in-built tidyverse data:

diamonds %>%
  group_by(cut, color) %>%
  summarise(price = mean(price))
# A tibble: 35 x 3
# Groups:   cut [5]
   cut   color price
   <ord> <ord> <dbl>
 1 Fair  D     4291.
 2 Fair  E     3682.
 3 Fair  F     3827.
 4 Fair  G     4239.
 5 Fair  H     5136.
 6 Fair  I     4685.
 7 Fair  J     4976.
 8 Good  D     3405.
 9 Good  E     3424.
10 Good  F     3496.
# ... with 25 more rows

This might require you to "reshape" your data, so also look at tidyr's pivot_longer() and pivot_wider() functions.

Or provide some of your data as a reproducible example and we'll be able to help you further!

1 Like

gt Datasets • gt (

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.