-
Notifications
You must be signed in to change notification settings - Fork 220
/
tidyverse_beauty_of_purrr1.Rmd
81 lines (57 loc) · 1.82 KB
/
tidyverse_beauty_of_purrr1.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
# tidyverse中的purrr之美1 {#tidyverse-beauty-of-purrr1}
```{r, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
fig.showtext = TRUE
)
```
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(broom)
penguins <- palmerpenguins::penguins %>% drop_na()
```
## 接、化、发
向马保国老师的武术精髓致敬。
```{r}
f <- list(
body_mass_g ~ bill_length_mm,
body_mass_g ~ bill_length_mm + bill_depth_mm,
body_mass_g ~ bill_length_mm + bill_depth_mm + flipper_length_mm
)
```
```{r, eval=FALSE}
f %>%
set_names(paste("model", 1:3)) %>% # 接
map( ~ tidy(lm(.x, data = penguins)) ) %>% # 化
list_rbind(names_to = "model") # 发
```
```{r, echo=FALSE}
f %>%
set_names(paste("model", 1:3)) %>%
map( ~ tidy(lm(.x, data = penguins)) ) %>%
list_rbind(names_to = "model") %>%
flextable::as_grouped_data(groups = "model") %>%
flextable::flextable() %>%
flextable::colformat_double(digits = 3)
```
也可以不用`broom::tidy()`, 而使用`gtsummary::tbl_regression()`
```{r, eval=FALSE}
library(gtsummary)
f %>%
set_names() %>% # 接
map(~ tbl_regression(lm(.x, data = penguins)) ) %>% # 化
tbl_merge(tab_spanner = paste("model", 1:3)) # 发
```
```{r, echo=FALSE}
library(gtsummary)
f %>%
set_names() %>% # 接
map(~ tbl_regression(lm(.x, data = penguins)) ) %>% # 化
tbl_merge(tab_spanner = paste("model", 1:3)) %>% # 发
as_flex_table()
```
```{r, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```