Compute the weighted mean of a single numeric variable in a survey design, with optional grouping, uncertainty quantification, and metadata-driven labelling.
get_means(
design,
x,
group = NULL,
variance = "ci",
conf_level = 0.95,
n_weighted = FALSE,
decimals = NULL,
min_cell_n = 30L,
na.rm = TRUE,
label_values = TRUE,
label_vars = TRUE,
name_style = "surveycore",
...,
.id = NULL,
.if_missing_var = NULL
)A survey_means tibble (also inheriting survey_result). Columns:
[group_cols...] — group variable columns (when active), first.
mean — weighted mean estimate.
Variance columns (se, var, cv, ci_low, ci_high, moe,
deff) — only those requested via variance.
n — unweighted count of non-NA observations used in the estimate.
n_weighted — sum of weights (only when requested).
The variable name is stored in meta(result)$variable, not as a column.
Use meta(result) to access design type, variable labels, and other
metadata.
A survey design object: survey_taylor, survey_replicate,
survey_twophase, or survey_nonprob.
<tidy-select> A single unquoted numeric
variable name. Must resolve to exactly one numeric column.
<tidy-select> Optional grouping
variable(s). Combined with any grouping set by group_by(). Default
NULL.
NULL or a character vector of one or more of "se",
"ci", "var", "cv", "moe", "deff". Controls which uncertainty
columns appear in the output. Default "ci".
Numeric scalar in (0, 1). Confidence level for intervals.
Default 0.95.
Logical. If TRUE, add an n_weighted column with the
sum of weights for non-NA observations in each group. Default FALSE.
Integer or NULL. If an integer, rounds all numeric output
columns (e.g., mean, se, ci_low, ci_high) to this many decimal
places. Default NULL (no rounding).
Integer. Minimum unweighted cell count before
surveycore_warning_small_cell fires. Default 30L (AAPOR guidance).
Logical. If TRUE (default), NA values are excluded from
analysis: observations where the analysis variable is NA are dropped
from calculations, and observations where any group variable is NA are
excluded from the output. If FALSE, NA observations in the analysis
variable are included in calculations, and observations where a group
variable is NA are collected into their own group row in the output
(appearing after all non-NA group rows).
Logical. Accepted for API uniformity; has no visible
effect since get_means() output contains no categorical value cells.
Default TRUE.
Logical. Accepted for API uniformity; has no visible
effect since get_means() output contains no variable-name value cells.
Default TRUE.
"surveycore" (default) or "broom". When "broom",
renames mean → estimate, se → std.error, etc.
Unused. Reserved so that .id and .if_missing_var remain
named-only when a survey_collection is passed as design.
Character(1) or NULL. Column name used to identify each
survey when design is a survey_collection. For collection inputs,
NULL (the default) resolves to the collection's stored @id property.
Pass a non-NULL value to override. Ignored when design is a single
survey.
"error", "skip", or NULL. How to handle
surveys in a collection that lack one of the requested NSE variables.
For collection inputs, NULL (the default) resolves to the collection's
stored @if_missing_var property. Pass a non-NULL value to override.
Ignored when design is a single survey.
Other analysis:
clean(),
get_anova(),
get_corr(),
get_covariance(),
get_diffs(),
get_freqs(),
get_pairwise(),
get_quantiles(),
get_ratios(),
get_t_test(),
get_totals(),
get_variance(),
meta()
d <- as_survey(nhanes_2017, ids = sdmvpsu, weights = wtint2yr,
strata = sdmvstra, nest = TRUE)
get_means(d, ridageyr)
# With grouped estimate
get_means(d, ridageyr, group = riagendr)
# AAPOR-compliant
get_means(d, ridageyr, variance = c("ci", "moe"), n_weighted = TRUE)
Run the code above in your browser using DataLab