Compute the estimated population total of a numeric variable in a survey design, or the estimated population size when no variable is supplied. Supports optional grouping, uncertainty quantification, and metadata-driven labelling.
get_totals(
design,
x = NULL,
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_totals tibble (also inheriting survey_result). Columns:
[group_cols...] — group variable columns (when active), first.
total — the weighted sum estimate.
Variance columns — only those requested via variance.
n — unweighted count (omitted in no-variable mode).
n_weighted — sum of weights (only when requested).
The variable name (or NULL for no-variable mode) is in
meta(result)$variable. Use meta(result) for additional metadata.
A survey design object: survey_taylor, survey_replicate,
survey_twophase, or survey_nonprob.
<tidy-select> Optional single unquoted
numeric variable name. When NULL (default), estimates the population
size (sum of weights). When supplied, estimates the weighted sum
(sum of w_i * x_i).
<tidy-select> Optional grouping
variable(s). Default NULL.
NULL or a character vector from "se", "ci", "var",
"cv", "moe", "deff". Default "ci".
Numeric scalar in (0, 1). Default 0.95.
Logical. For get_totals(d) (no variable), equals the
total column and is included for API uniformity. For variable mode,
adds the sum of weights for non-NA observations. Default FALSE.
Integer or NULL. If an integer, rounds all numeric output
columns (e.g., total, se, ci_low, ci_high) to this many decimal
places. Default NULL (no rounding).
Integer. Default 30L.
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. Default TRUE.
Logical. Accepted for API uniformity. Default TRUE.
"surveycore" (default) or "broom".
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_means(),
get_pairwise(),
get_quantiles(),
get_ratios(),
get_t_test(),
get_variance(),
meta()
d <- as_survey_replicate(acs_pums_wy, weights = pwgtp,
repweights = pwgtp1:pwgtp80,
type = "successive-difference")
# Population size
get_totals(d)
# Total for a variable
get_totals(d, agep)
# Grouped
get_totals(d, agep, group = sex)
Run the code above in your browser using DataLab