Rob Hanssen
Rob Hanssen

Reputation: 183

summary row of a table (sum of a column)

Total brainfart, but how can I add a summary row at the bottom of a table that is the sum of all values in the column - while retaining all the other values?

Desired outcome:

# A tibble: 4 x 6
  compound `2023` `2024` `2025` `2026` `2027`
  <chr>     <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 A          211    306    427    548    631
2 B         6410   9132  12664  16187  18668
3 C         2598   3764   5250   6735   7760
4 total     9220  13202  18341  23469  27059

This one was created with a bind_rows() of the table and a summarized version of the table, but I can't image there not being a better way to do this.

Upvotes: 0

Views: 350

Answers (1)

Luiy_coder
Luiy_coder

Reputation: 321

Use adorn_totals from Janitor package.

library(janitor)
compound = c("A", "B", "C")
a = c(10, 20, 30)
b = c(199, 299, 399)


data = tibble(compound, a, b)

data %>% adorn_totals("row")

Sample Output

Upvotes: 2

Related Questions