-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
group-rows.Rmd
95 lines (71 loc) · 1.77 KB
/
group-rows.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
title: "Group rows"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Group rows}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
This vignette introduces nice and easy way to display grouped data frame created by `dplyr::group_by`.
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = "dplyr" %in% rownames(installed.packages())
)
```
```{r setup, include=FALSE}
library(ftExtra)
library(dplyr)
```
```{r setup2, ref.label='setup', eval=FALSE}
```
```{r}
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
```{r}
grouped_iris %>% as_flextable()
```
```{r}
grouped_iris %>% as_flextable(hide_grouplabel = TRUE)
```
## Multiple grouping columns
```{r}
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
```{r}
grouped_iris %>%
as_flextable(groups_to = "merged")
```
## Multiple grouping variables
```{r}
grouped_mtcars %>%
as_flextable(groups_to = "merged", groups_arrange = TRUE)
```
```{r}
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'`.
```{r}
grouped_mtcars %>%
as_flextable(groups_to = "merged", groups_pos = "asis") %>%
flextable::theme_vanilla()
```