dplyr::rename_with
presenter::format_percent
diamonds %>%
select(-clarity) %>%
mutate(relative_price = price / max(price), .before = "x") %>%
group_by(across(where(is.ordered))) %>%
summarize(across(where(is.double), mean), .groups = "drop") %>%
format_percent(relative_price) %>%
rename_with(~str_c("category_", .), cut:color) %>%
rename_with(~str_c("property_", .), carat:table) %>%
rename_with(~str_c("dimension_", .), x:z) -> diamonds_summary
diamonds_summary %>% head
#> # A tibble: 6 × 9
#> category_cut category_color property_carat property_depth property_table
#> <ord> <ord> <dbl> <dbl> <dbl>
#> 1 Fair D 0.920 64.0 59.0
#> 2 Fair E 0.857 63.3 59.4
#> 3 Fair F 0.905 63.5 59.5
#> 4 Fair G 1.02 64.3 58.8
#> 5 Fair H 1.22 64.6 58.7
#> 6 Fair I 1.20 64.2 59.2
#> # … with 4 more variables: relative_price <formttbl>, dimension_x <dbl>,
#> # dimension_y <dbl>, dimension_z <dbl>
Header words are automatically identified by the following regular expression: "^.*(?=(_|\\.))"
. Leaving the header_word
argument blank produces the same result as providing the commented out argument below. The last id column controls merging and greyscaling. Numeric columns are given some automatic formatting within the make_flextable
function.
diamonds_summary %>%
make_flextable(last_id_col = 2,
# header_words = c("category", "property", "dimension"),
theme = "zebra_gold")
category |
property |
relative_price |
dimension |
|||||
cut |
color |
carat |
depth |
table |
x |
y |
z |
|
Fair |
D |
0.92 |
64.05 |
58.97 |
23% |
6.02 |
5.96 |
3.84 |
E |
0.86 |
63.32 |
59.36 |
20% |
5.91 |
5.86 |
3.72 |
|
F |
0.90 |
63.51 |
59.45 |
20% |
5.99 |
5.93 |
3.79 |
|
G |
1.02 |
64.34 |
58.77 |
23% |
6.17 |
6.11 |
3.96 |
|
H |
1.22 |
64.59 |
58.70 |
27% |
6.58 |
6.50 |
4.22 |
|
I |
1.20 |
64.22 |
59.24 |
25% |
6.56 |
6.49 |
4.19 |
|
J |
1.34 |
64.36 |
58.92 |
26% |
6.75 |
6.68 |
4.32 |
|
Good |
D |
0.74 |
62.37 |
58.54 |
18% |
5.62 |
5.63 |
3.50 |
E |
0.75 |
62.20 |
58.78 |
18% |
5.62 |
5.63 |
3.50 |
|
F |
0.78 |
62.20 |
58.91 |
19% |
5.69 |
5.71 |
3.54 |
|
G |
0.85 |
62.53 |
58.47 |
22% |
5.85 |
5.86 |
3.65 |
|
H |
0.91 |
62.50 |
58.61 |
23% |
5.97 |
5.97 |
3.73 |
|
I |
1.06 |
62.48 |
58.77 |
27% |
6.25 |
6.26 |
3.90 |
|
J |
1.10 |
62.40 |
58.81 |
24% |
6.38 |
6.39 |
3.98 |
|
Very Good |
D |
0.70 |
61.75 |
58.04 |
18% |
5.50 |
5.53 |
3.41 |
E |
0.68 |
61.73 |
58.04 |
17% |
5.43 |
5.46 |
3.37 |
|
F |
0.74 |
61.72 |
57.85 |
20% |
5.61 |
5.64 |
3.47 |
|
G |
0.77 |
61.84 |
57.78 |
21% |
5.66 |
5.69 |
3.51 |
|
H |
0.92 |
61.97 |
57.90 |
24% |
5.98 |
6.01 |
3.71 |
|
I |
1.05 |
61.93 |
58.11 |
28% |
6.27 |
6.30 |
3.89 |
|
J |
1.13 |
61.90 |
58.28 |
27% |
6.46 |
6.50 |
4.01 |
|
Premium |
D |
0.72 |
61.17 |
58.72 |
19% |
5.60 |
5.57 |
3.42 |
E |
0.72 |
61.18 |
58.78 |
19% |
5.59 |
5.56 |
3.41 |
|
F |
0.83 |
61.26 |
58.68 |
23% |
5.88 |
5.84 |
3.59 |
|
G |
0.84 |
61.28 |
58.70 |
24% |
5.86 |
5.83 |
3.58 |
|
H |
1.02 |
61.32 |
58.79 |
28% |
6.24 |
6.23 |
3.81 |
|
I |
1.14 |
61.33 |
58.77 |
32% |
6.49 |
6.45 |
3.96 |
|
J |
1.29 |
61.39 |
58.87 |
33% |
6.81 |
6.77 |
4.17 |
|
Ideal |
D |
0.57 |
61.68 |
55.97 |
14% |
5.19 |
5.20 |
3.20 |
E |
0.58 |
61.69 |
55.97 |
14% |
5.22 |
5.23 |
3.22 |
|
F |
0.66 |
61.68 |
55.92 |
18% |
5.41 |
5.42 |
3.34 |
|
G |
0.70 |
61.70 |
55.90 |
20% |
5.51 |
5.52 |
3.40 |
|
H |
0.80 |
61.73 |
55.97 |
21% |
5.73 |
5.74 |
3.54 |
|
I |
0.91 |
61.79 |
56.02 |
24% |
5.98 |
5.99 |
3.70 |
|
J |
1.06 |
61.82 |
56.01 |
26% |
6.32 |
6.33 |
3.91 |
tibble(x = letters[1:10],
y = -5:4,
z = -c(-.5, -.2, -.1, 0, .1, .2, .3, .4, .6, .9)) %>%
format_percent(z) %>%
rename_with(~str_c("sample_", .)) %>%
make_flextable()
sample | ||
x |
y |
z |
a |
-5 |
50% |
b |
-4 |
20% |
c |
-3 |
10% |
d |
-2 |
-0% |
e |
-1 |
-10% |
f |
0 |
-20% |
g |
1 |
-30% |
h |
2 |
-40% |
i |
3 |
-60% |
j |
4 |
-90% |
tibble(x = letters[1:10],
y = -5:4,
z = -c(-.5, -.2, -.1, 0, .1, .2, .3, .4, .6, .9)) %>%
format_percent(z) %>%
make_flextable()
x |
y |
z |
a |
-5 |
50% |
b |
-4 |
20% |
c |
-3 |
10% |
d |
-2 |
-0% |
e |
-1 |
-10% |
f |
0 |
-20% |
g |
1 |
-30% |
h |
2 |
-40% |
i |
3 |
-60% |
j |
4 |
-90% |