Group rows

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

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()

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species: setosa

5.1

3.5

1.4

0.2

4.9

3.0

1.4

0.2

Species: versicolor

7.0

3.2

4.7

1.4

6.4

3.2

4.5

1.5

Species: virginica

6.3

3.3

6.0

2.5

5.8

2.7

5.1

1.9

grouped_iris %>% as_flextable(hide_grouplabel = TRUE)

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

setosa

5.1

3.5

1.4

0.2

4.9

3.0

1.4

0.2

versicolor

7.0

3.2

4.7

1.4

6.4

3.2

4.5

1.5

virginica

6.3

3.3

6.0

2.5

5.8

2.7

5.1

1.9

Multiple grouping columns

grouped_mtcars %>% as_flextable()

model

mpg

disp

am: 1.0

cyl: 6.0

Mazda RX4

21.0

160

Mazda RX4 Wag

21.0

160

am: 1.0

cyl: 4.0

Datsun 710

22.8

108

am: 0.0

cyl: 6.0

Hornet 4 Drive

21.4

258

am: 0.0

cyl: 8.0

Hornet Sportabout

18.7

360

am: 0.0

cyl: 6.0

Valiant

18.1

225

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")
#> Warning in .Deprecated("flextable:::as_flextable.data.frame", msg =
#> paste("ftExtra:::as_flextable.data.frame is deprecated", :
#> ftExtra:::as_flextable.data.frame is deprecated and will be removed in the
#> future release. Consider using flextalbe's implementation by running
#> `.S3method("as_flextable", "data.frame", flextable:::as_flextable.data.frame)`

Species

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

setosa

5.1

3.5

1.4

0.2

4.9

3.0

1.4

0.2

versicolor

7.0

3.2

4.7

1.4

6.4

3.2

4.5

1.5

virginica

6.3

3.3

6.0

2.5

5.8

2.7

5.1

1.9

Multiple grouping variables

grouped_mtcars %>%
  as_flextable(groups_to = "merged", groups_arrange = TRUE)
#> Warning in .Deprecated("flextable:::as_flextable.data.frame", msg =
#> paste("ftExtra:::as_flextable.data.frame is deprecated", :
#> ftExtra:::as_flextable.data.frame is deprecated and will be removed in the
#> future release. Consider using flextalbe's implementation by running
#> `.S3method("as_flextable", "data.frame", flextable:::as_flextable.data.frame)`

am

cyl

model

mpg

disp

0

6

Hornet 4 Drive

21.4

258

Valiant

18.1

225

8

Hornet Sportabout

18.7

360

1

4

Datsun 710

22.8

108

6

Mazda RX4

21.0

160

Mazda RX4 Wag

21.0

160

grouped_mtcars %>%
  as_flextable(groups_to = "merged", groups_arrange = FALSE)
#> Warning in .Deprecated("flextable:::as_flextable.data.frame", msg =
#> paste("ftExtra:::as_flextable.data.frame is deprecated", :
#> ftExtra:::as_flextable.data.frame is deprecated and will be removed in the
#> future release. Consider using flextalbe's implementation by running
#> `.S3method("as_flextable", "data.frame", flextable:::as_flextable.data.frame)`

am

cyl

model

mpg

disp

1

6

Mazda RX4

21.0

160

Mazda RX4 Wag

21.0

160

4

Datsun 710

22.8

108

0

6

Hornet 4 Drive

21.4

258

8

Hornet Sportabout

18.7

360

6

Valiant

18.1

225

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()
#> Warning in .Deprecated("flextable:::as_flextable.data.frame", msg =
#> paste("ftExtra:::as_flextable.data.frame is deprecated", :
#> ftExtra:::as_flextable.data.frame is deprecated and will be removed in the
#> future release. Consider using flextalbe's implementation by running
#> `.S3method("as_flextable", "data.frame", flextable:::as_flextable.data.frame)`

model

cyl

mpg

disp

am

Mazda RX4

6

21.0

160

1

Mazda RX4 Wag

21.0

160

Datsun 710

4

22.8

108

Hornet 4 Drive

6

21.4

258

0

Hornet Sportabout

8

18.7

360

Valiant

6

18.1

225