- Get Data Into the Right Format to Create your First Table
- Use better column names and a title
- Align columns
- Use groups instead of repetitive columns
- Format your table's numbers
- Add summaries
- Add additional horizontal lines
- Add background colors
- Change the text appearance
- Change cell properties
- Export Your Tables
- Heat map columns
- Adding Charts with Flextable
- Add your own ggplot
- Case Study
Making Beautiful Tables with R
Format your table's numbers
This lesson is called Format your table's numbers, part of the Making Beautiful Tables with R course. This lesson is called Format your table's numbers, 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.
Notice that our table contains a lot of missing numbers. That’s fine but leaving a lot of white space may be off putting to the reader. So, let’s add small dashes to the empty cells.
This demonstrates how to format missing numbers, but it also explains how format any kind of data. Also, this will introduce formulas for selecting specific cells in the table.
penguin_counts_wider |>
mutate(island = paste('Island: ', island)) |>
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 = 'Penguins in the Palmer Archipelago\nData is courtesy of the {palmerpenguins} R package'
) |>
align(i = 2, align = 'center', part = 'header') |>
colformat_num(i = ~ (is.na(island)), na_str = '-') |>
autofit()
Your Turn
Round the numbers to two decimals with colformat_double()
. Your table should look like this:
You need to be signed-in to comment on this post. Login.
soundarya soundararajan
October 18, 2023
Hi, in this exercise, when I used the colformat_double(digits = 2) function, I seem to get the digits rounded off, but everything ended with 0 instead of what I see here. For eg, instead of 44.44 for Egypt 1957 I got 44.40.
Albert Rapp
October 18, 2023
Hi there, that's a bit odd. Can you check what value you get when you extract the specific life expectancy for Egypt?` Try running this code: