mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440
You can also specify the width of the indentation by the level_of_indent option. At the same time, if you
want to indent every column, you can choose to turn on all_cols. Note that if a column is right aligned,
you probably won’t be able to see the effect.
kbl(dt, booktabs = T, align = "l") %>%
add_indent(c(1, 3, 5), level_of_indent = 2, all_cols = T)
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440
Group rows via multi-row cell
Function pack_rows is great for showing simple structural information on rows but sometimes people
may need to show structural information with multiple layers. When it happens, you may consider us-
ing collapse_rows instead, which will put repeating cells in columns into multi-row cells.
In LaTeX, collapse_rows adds some extra hlines to help differentiate groups. You can customize this
behavior using the latex_hline argument. You can choose from full (default), major and none.
Vertical alignment of cells (with the default row_group_label_position = "identity")) is controlled by
the valign option. You can choose from “top”, “middle” (default) and “bottom”. Be cautious that the
vertical alignment option was only introduced in multirow in 2016. If you are using a legacy LaTeX dis-
tribution, you will run into trouble if you set valign to be either “top” or “bottom”. Alternatively, use
row_group_label_position = "first", which will put the row group labels into the first row without
using the \multirow LaTeX command at all.
collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
C3 = 1:15,
C4 = sample(c(0,1), 15, replace = TRUE))
kbl(collapse_rows_dt, booktabs = T, align = "c") %>%
column_spec(1, bold=T) %>%
collapse_rows(columns = 1:2, latex_hline = "major", row_group_label_position = "first")
21