Making Beautiful Tables with R
Add summaries
This lesson is called Add summaries, part of the Making Beautiful Tables with R course. This lesson is called Add summaries, part of the Making Beautiful Tables with R course.
Transcript
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
Loading transcript...
It’s always good to not just show the data, but to also to show a summary of the data. In this case, it could be something as simple as the maximum number of penguins of a specific species living on an island over the years.
maximum_summary <- penguin_counts_wider |>
group_by(island) |>
summarise(across(
.cols = -year,
.fns = max
)) |>
mutate(year = 'Maximum')
total_summary <- penguin_counts_wider |>
group_by(island) |>
summarise(across(
.cols = -year,
.fns = sum
)) |>
mutate(year = 'Total')
bind_rows(penguin_counts_wider, maximum_summary, total_summary) |>
mutate(island = paste('Island: ', island)) |>
arrange(island, year) |>
as_grouped_data(groups = 'island') |>
as_flextable(hide_grouplabel = TRUE) |>
set_header_labels(
island = 'Island',
year = '',
Adelie_female = 'Female',
Adelie_male = 'Male',
Chinstrap_female = 'Female',
Chinstrap_male = 'Male',
Gentoo_female = 'Female',
Gentoo_male = 'Male'
) |>
add_header_row(
values = c('', 'Adelie', 'Chinstrap', 'Gentoo'),
colwidths = c(1, 2, 2, 2)
) |>
add_header_lines(
values = c('Penguins in the Palmer Archipelago', 'Data is courtesy of the {palmerpenguins} R package')
) |>
align(i = 3, align = 'center', part = 'header') |>
colformat_num(i = ~ (is.na(island)), na_str = '-') |>
align(
i = ~ (year %in% 2007:2009),
j = 'year',
align = 'right'
) |>
autofit()
Your Turn
For each continent, add the average and maximum life expectancy for each year to the table. Your table should look like this:
Have any questions? Put them below and we will help you out!
Course Content
16 Lessons
You need to be signed-in to comment on this post. Login.