Group rows

This vignette introduces nice and easy way to display grouped data frame created by dplyr::group_by.

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = "dplyr" %in% rownames(installed.packages())
)
library(ftExtra)
library(dplyr)

grouped_iris <- iris %>%
  group_by(Species) %>%
  slice(1, 2)

grouped_mtcars <- mtcars %>%
  mutate(model = rownames(mtcars)) %>%
  head %>%
  select(model, cyl, mpg, disp, am) %>%
  group_by(am, cyl)

Groups as titles

Single grouping columns

grouped_iris %>% as_flextable()
grouped_iris %>% as_flextable(hide_grouplabel = TRUE)

Multiple grouping columns

grouped_mtcars %>% as_flextable()

Groups as merged columns

By specifying as_flextable(groups_to = 'merged'), grouping variables are merged vertically. In this case, the default theme is changed to flextable::theme_vanilla because the booktab theme is not intuitive.

Single grouping variable

grouped_iris %>%
  as_flextable(groups_to = "merged")

Multiple grouping variables

grouped_mtcars %>%
  as_flextable(groups_to = "merged", groups_arrange = TRUE)
grouped_mtcars %>%
  as_flextable(groups_to = "merged", groups_arrange = FALSE)

Position of grouping variables

Grouping variables are moved to left by default. If you want to keep their positions, specify group_pos = 'asis'.

grouped_mtcars %>%
  as_flextable(groups_to = "merged", groups_pos = "asis") %>%
  flextable::theme_vanilla()


Try the ftExtra package in your browser

Any scripts or data that you put into this service are public.

ftExtra documentation built on Sept. 29, 2023, 9:06 a.m.