Skip to content
R for the Rest of Us Logo

This lesson is locked

Get access to all lessons in this course.

If the video is not playing correctly, you can watch it in a new window

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.

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!

You need to be signed-in to comment on this post. Login.