Using a subset of the gtcars dataset, let’s create a simple gt table with row labels (from the model column) inside of a stub. This eight-row table begins with no row groups at all but with a single use of tab_row_group(), we can specify a row group that will contain any rows where the car model begins with a number.
This actually makes two row groups since there are row labels that don’t begin with a number. That second row group is a catch-all NA group, and it doesn’t display a label at all. Rather, it is set off from the other group with a double line. This may be a preferable way to display the arrangement of one distinct group and an ‘others’ or default group. If that’s the case but you’d like the order reversed, you can use row_group_order().
Two more options include: (1) setting a default label for the ‘others’ group (done through tab_options()), and (2) creating row groups until there are no more unaccounted for rows. Let’s try the first option in the next example:
The above use of the row_group.default_label in tab_options() gets the job done and provides a default label. One drawback is that the default/NA group doesn’t have an ID, so it can’t as easily be styled with tab_style(); however, row groups have indices and the index for the "others" group here is 1.
Now let’s try using tab_row_group() with our gtcars-based table such that all rows are formally assigned to different row groups. We’ll define two row groups with the (Markdown-infused) labels "**Powerful Cars**" and "**Super Powerful Cars**". The distinction between the groups is whether hp is lesser or greater than 600 (and this is governed by the expressions provided to the rows argument).
Setting the id values for each of the row groups makes things easier since you will have clean, markup-free ID values to reference in later calls (as was done with the tab_style() invocations in the example above). The use of the md() helper function makes it so that any Markdown provided for the label of a row group is faithfully rendered.