Created
August 8, 2021 20:43
-
-
Save TimTeaFan/8f89fd127c1b38368d6dba3c2cafea46 to your computer and use it in GitHub Desktop.
Add rows with total and group means in dplyr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
# Add Total Mean for numeric variables | |
iris %>% | |
add_row( | |
summarise(., | |
Species = "Total mean", | |
across(where(is.numeric), mean)) | |
) %>% | |
tail() # for printing | |
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species | |
#> 146 6.700000 3.000000 5.200 2.300000 virginica | |
#> 147 6.300000 2.500000 5.000 1.900000 virginica | |
#> 148 6.500000 3.000000 5.200 2.000000 virginica | |
#> 149 6.200000 3.400000 5.400 2.300000 virginica | |
#> 150 5.900000 3.000000 5.100 1.800000 virginica | |
#> 151 5.843333 3.057333 3.758 1.199333 Total mean | |
library(dplyr) | |
# Add group mean as new row to data | |
iris %>% | |
group_by(Species) %>% | |
group_map(.keep = TRUE, | |
~ add_row(.x, | |
summarise(.x, | |
Species = paste0(unlist(.y), " mean"), | |
across(where(is.numeric), mean), | |
) | |
)) %>% | |
bind_rows() %>% | |
tail # for printing | |
#> # A tibble: 6 x 5 | |
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species | |
#> <dbl> <dbl> <dbl> <dbl> <chr> | |
#> 1 6.7 3 5.2 2.3 virginica | |
#> 2 6.3 2.5 5 1.9 virginica | |
#> 3 6.5 3 5.2 2 virginica | |
#> 4 6.2 3.4 5.4 2.3 virginica | |
#> 5 5.9 3 5.1 1.8 virginica | |
#> 6 6.59 2.97 5.55 2.03 virginica mean | |
library(dplyr) | |
# Summarise by group and by total | |
iris %>% | |
bind_rows(., | |
mutate(., Species = "all")) %>% | |
group_by(Species) %>% | |
summarise(across(.f = mean)) | |
#> # A tibble: 4 x 5 | |
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width | |
#> <chr> <dbl> <dbl> <dbl> <dbl> | |
#> 1 all 5.84 3.06 3.76 1.20 | |
#> 2 setosa 5.01 3.43 1.46 0.246 | |
#> 3 versicolor 5.94 2.77 4.26 1.33 | |
#> 4 virginica 6.59 2.97 5.55 2.03 | |
# add mean for each groups and total mean as new row to data | |
library(dplyr) | |
iris %>% | |
bind_rows(., | |
mutate(., Species = "all")) %>% | |
group_by(Species) %>% | |
group_map(.keep = TRUE, | |
~ add_row(.x, | |
summarise(.x, | |
Species = paste0(unlist(.y), " mean"), | |
across(where(is.numeric), mean), | |
) | |
)) %>% | |
bind_rows() %>% | |
group_by(Species) %>% # --| | |
slice_tail(n = 2) # ------| for printing | |
#> # A tibble: 12 x 5 | |
#> # Groups: Species [8] | |
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species | |
#> <dbl> <dbl> <dbl> <dbl> <chr> | |
#> 1 6.2 3.4 5.4 2.3 all | |
#> 2 5.9 3 5.1 1.8 all | |
#> 3 5.84 3.06 3.76 1.20 all mean | |
#> 4 5.3 3.7 1.5 0.2 setosa | |
#> 5 5 3.3 1.4 0.2 setosa | |
#> 6 5.01 3.43 1.46 0.246 setosa mean | |
#> 7 5.1 2.5 3 1.1 versicolor | |
#> 8 5.7 2.8 4.1 1.3 versicolor | |
#> 9 5.94 2.77 4.26 1.33 versicolor mean | |
#> 10 6.2 3.4 5.4 2.3 virginica | |
#> 11 5.9 3 5.1 1.8 virginica | |
#> 12 6.59 2.97 5.55 2.03 virginica mean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment