Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
Data Summarization: Grouped summaries
Grouped summaries
Sometimes we want to compare summary statistics for different groups of people in our dataset
You can use
group_by()to specify the grouping variable(s) before summarizingThen you can use another summary function to calculate the statistics for each group separately
We will review the functions that can be used for grouped summaries:
summarise()fromdplyrskim()fromskimrget_summary_stats()fromrstatixtbl_summary()fromgtsummary- Slightly different: does not use
group_by()
- Slightly different: does not use
- For each, we will look at grouped summaries for:
- Grouped by
sex - Summarize
age_yr,cesd, andincome
- Grouped by
New summary function: summarise()/summarize()
- Not necessarily grouped
- Flexible function that allows you to calculate any summary statistic you want, and name the resulting columns yourself
- You need to feed it a column and a function to apply to that column
- Really flexible: you can calculate custom summary statistics
- More work if you want general summary statistics: you have to write out each statistic and name each resulting column yourself
Example: summary statistics on age
- For each summary statistic, I need to use the designated function and specify the column I want to summarize
hrs_00 |>
summarise(
mean_age = mean(age_yr, na.rm = TRUE),
1 mean_age_above_80 = mean(age_yr[age_yr > 80], na.rm = TRUE),
median_age = median(age_yr, na.rm = TRUE),
sd_age = sd(age_yr, na.rm = TRUE),
min_age = min(age_yr, na.rm = TRUE),
max_age = max(age_yr, na.rm = TRUE),
)- 1
- I can create a unique summary statistic without changing the dataset. I have calculated the mean age for people above 80 years old.
mean_age mean_age_above_80 median_age sd_age min_age max_age
1 58.82075 85.73333 55 9.499722 34 100
Group by a variable in summarise()
- 1
-
Use
group_by()to specify the grouping variable before summarizing. The summary statistics will be calculated separately for each level ofsex. - 2
-
Use
summarise()to calculate the summary statistics. Syntax will be the same as non-grouped summaries.
# A tibble: 2 × 7
sex mean_age sd_age mean_cesd sd_cesd mean_income sd_income
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Female 59.0 10.3 1.92 2.20 82243. 106415.
2 Male 58.6 8.28 1.61 2.02 104748. 152653.
Group by a variable in skim()
- 1
-
Use
group_by()to specify the grouping variable before summarizing. The summary statistics will be calculated separately for each level ofsex. - 2
-
Use
select()to specify the variables you want to summarize. - 3
-
Use
skim()to calculate the summary statistics. Syntax will be the same as non-grouped summaries.
Adding missing grouping variables: `sex`
| Name | select(group_by(hrs_00, s… |
| Number of rows | 2728 |
| Number of columns | 4 |
| _______________________ | |
| Column type frequency: | |
| numeric | 3 |
| ________________________ | |
| Group variables | sex |
Variable type: numeric
| skim_variable | sex | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|---|
| age_yr | Female | 0 | 1 | 58.96 | 10.31 | 34 | 52.00 | 55.0 | 65 | 100 | ▁▇▃▁▁ |
| age_yr | Male | 0 | 1 | 58.63 | 8.28 | 40 | 53.00 | 56.0 | 64 | 92 | ▁▇▃▁▁ |
| cesd | Female | 4 | 1 | 1.92 | 2.20 | 0 | 0.00 | 1.0 | 3 | 8 | ▇▃▁▁▁ |
| cesd | Male | 3 | 1 | 1.61 | 2.02 | 0 | 0.00 | 1.0 | 2 | 8 | ▇▂▁▁▁ |
| income | Female | 0 | 1 | 82243.22 | 106415.42 | 0 | 18230.09 | 47326.0 | 102785 | 990200 | ▇▁▁▁▁ |
| income | Male | 0 | 1 | 104748.19 | 152652.90 | 0 | 22084.50 | 58734.8 | 126650 | 1800012 | ▇▁▁▁▁ |
Group by a variable in get_summary_stats()
- 1
-
Use
group_by()to specify the grouping variable before summarizing. The summary statistics will be calculated separately for each level ofsex. - 2
-
Use
get_summary_stats()to calculate the summary statistics. Syntax will be the same as non-grouped summaries.
# A tibble: 6 × 11
sex variable n min max median iqr mean sd se ci
<fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Female age_yr 1568 34 1 e2 55 1.3 e1 5.90e1 1.03e1 2.6 e-1 5.11e-1
2 Female cesd 1564 0 8 e0 1 3 e0 1.92e0 2.20e0 5.6 e-2 1.09e-1
3 Female income 1568 0 9.90e5 47326 8.46e4 8.22e4 1.06e5 2.69e+3 5.27e+3
4 Male age_yr 1160 40 9.2 e1 56 1.1 e1 5.86e1 8.28e0 2.43e-1 4.77e-1
5 Male cesd 1157 0 8 e0 1 2 e0 1.61e0 2.02e0 5.9 e-2 1.17e-1
6 Male income 1160 0 1.80e6 58735. 1.05e5 1.05e5 1.53e5 4.48e+3 8.79e+3
Group by a variable in tbl_summary()
- Slightly different syntax than the other functions: does not use
group_by() by =splits the table into side-by-side columns for each group
hrs_00 |>
select(age_yr, cesd, income, sex) |>
1 tbl_summary(by = sex)- 1
-
Stratify the whole table by
sex, creating one column per group
| Characteristic | Female N = 1,5681 |
Male N = 1,1601 |
|---|---|---|
| Age at Interview (Years) | 55 (52, 65) | 56 (53, 64) |
| CES-D Depression Score | ||
| 0 | 543 (35%) | 450 (39%) |
| 1 | 357 (23%) | 300 (26%) |
| 2 | 211 (13%) | 135 (12%) |
| 3 | 116 (7.4%) | 84 (7.3%) |
| 4 | 92 (5.9%) | 60 (5.2%) |
| 5 | 78 (5.0%) | 44 (3.8%) |
| 6 | 70 (4.5%) | 35 (3.0%) |
| 7 | 71 (4.5%) | 22 (1.9%) |
| 8 | 26 (1.7%) | 27 (2.3%) |
| Unknown | 4 | 3 |
| Total Household Income | 47,326 (18,229, 102,785) | 58,735 (22,059, 126,700) |
| 1 Median (Q1, Q3); n (%) | ||
Wrap-up
- We have covered several functions for summarizing grouped data in R
summarise()skim()get_summary_stats()tbl_summary()