Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I add a Total Row for each value in a specific column, that does calculations based upon other columns,

Tags:

r

Assume I have this data frame

enter image description here

What I want is this

enter image description here

What I want to do is create rows which groups upon the month variable, which then obtains the sum of the total variable, and the unique value of the days_month variable for all of the values in person for that month.

I am just wondering if there is an easy way to do this that does not involve multiple spreads and gathers with adorn totals that I have to change the days in month back to original value after the totals were summed, etc. Is there a quick and easy way to do this?

like image 813
Mark Avatar asked Sep 15 '25 01:09

Mark


1 Answers

One option would be to group by 'month', 'days_in_month' and apply adorn_total by group_mapping

library(dplyr)
library(janitor)
df1 %>% 
    group_by(month, days_in_month) %>%
    group_map(~ .x %>%
                  adorn_totals("row"))  %>%
    select(names(df1))
# A tibble: 10 x 4
# Groups:   month, days_in_month [2]
#   month person total days_in_month
#   <int> <chr>  <int>         <int>
# 1     1 John       7            31
# 2     1 Jane      18            31
# 3     1 Tim       20            31
# 4     1 Cindy     11            31
# 5     1 Total     56            31
# 6     2 John      18            28
# 7     2 Jane      13            28
# 8     2 Tim       15            28
# 9     2 Cindy      9            28
#10     2 Total     55            28

If we need other statistics, we can have it in group_map

library(tibble)
df1 %>% 
  group_by(month, days_in_month) %>% 
  group_map(~ bind_rows(.x, tibble(person = "Mean", total = mean(.x$total))))

data

df1 <- structure(list(month = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), person = c("John", 
   "Jane", "Tim", "Cindy", "John", "Jane", "Tim", "Cindy"), total = c(7L, 
 18L, 20L, 11L, 18L, 13L, 15L, 9L), days_in_month = c(31L, 31L, 
  31L, 31L, 28L, 28L, 28L, 28L)), class = "data.frame", row.names = c(NA, 
-8L))
like image 121
akrun Avatar answered Sep 16 '25 20:09

akrun