Analogous to tally and count, calculates the survey weighted count of observations. survey_tally will call survey_total empty (resulting in the count of each group) or on wt if it is specified (resulting in the survey weighted total of wt). survey_count is similar, but calls group_by before calculating the count and then returns the data to the original groupings.

survey_tally(
  x,
  wt,
  sort = FALSE,
  name = "n",
  vartype = c("se", "ci", "var", "cv")
)

survey_count(
  x,
  ...,
  wt = NULL,
  sort = FALSE,
  name = "n",
  .drop = dplyr::group_by_drop_default(x),
  vartype = c("se", "ci", "var", "cv")
)

Arguments

x

A tbl_svy object, as created by as_survey and related functions.

wt

(Optional) A variable to weight on (in addition to the survey weights, which are always used). If left unspecified, tally() will use a variable named "n" if one exists, but count() will not. Override this behavior by specifying wt = NULL.

sort

Whether to sort the results (defaults to FALSE)

name

Name of count variable created (defaults to n). If the variable already exists, will add "n" to the end until it does not.

vartype

What types variation estimates to calculate, passed to survey_total.

...

Variables to group by, passed to group_by().

.drop

When .drop = TRUE, empty groups are dropped, see group_by documentation for more details.

Details

If n already exists, tally will use it as the weight, but count will not.

Examples

library(survey)
data(api)

dstrata <- apistrat %>%
  as_survey_design(strata = stype, weights = pw)

dstrata %>%
  group_by(awards) %>%
  survey_tally()
#> # A tibble: 2 × 3
#>   awards     n  n_se
#>   <fct>  <dbl> <dbl>
#> 1 No     2236.  216.
#> 2 Yes    3958.  216.

dstrata %>%
  survey_count(awards)
#> # A tibble: 2 × 3
#>   awards     n  n_se
#>   <fct>  <dbl> <dbl>
#> 1 No     2236.  216.
#> 2 Yes    3958.  216.