This function gives you a simple count with percentages that is weighted

ct(df, var, wt, cum = FALSE, show_na = TRUE)

Arguments

df

Name of the Dataset

var

Variable to Count

wt

Weighting Variable

cum

Will add a cumulative total if set to TRUE

show_na

Will remove the NAs before the calculation if set to TRUE, defaults to FALSE

Examples

cces <- read_csv("https://raw.githubusercontent.com/ryanburge/blocks/master/cces.csv")
#> Warning: Missing column names filled in: 'X1' [1]
#> Parsed with column specification: #> cols( #> X1 = col_double(), #> V101 = col_double(), #> race = col_double(), #> gender = col_double(), #> commonweight_vv = col_double() #> )
cces %>% ct(race)
#> # A tibble: 8 x 3 #> race n pct #> <dbl> <int> <dbl> #> 1 1 368 0.736 #> 2 2 54 0.108 #> 3 3 38 0.076 #> 4 4 13 0.026 #> 5 5 7 0.014 #> 6 6 9 0.018 #> 7 7 8 0.016 #> 8 8 3 0.006
# With a weight cces %>% ct(race, wt = commonweight_vv)
#> # A tibble: 8 x 3 #> race n pct #> <dbl> <dbl> <dbl> #> 1 1 348. 0.758 #> 2 2 43.9 0.096 #> 3 3 34.0 0.074 #> 4 4 7.09 0.015 #> 5 5 3.87 0.008 #> 6 6 15.2 0.033 #> 7 7 6.33 0.014 #> 8 8 0.704 0.002
cces %>% mutate(race2 = frcode(race == 1 ~ "White", race == 2 ~ "Black", race == 3 ~ "Hispanic", race == 4 ~ "Asian")) %>% ct(race2, show_na = FALSE, commonweight_vv)
#> # A tibble: 4 x 3 #> race2 n pct #> <fct> <dbl> <dbl> #> 1 White 348. 0.804 #> 2 Black 43.9 0.101 #> 3 Hispanic 34.0 0.078 #> 4 Asian 7.09 0.016
# Cumulative Counts cces %>% ct(race, cum = TRUE)
#> # A tibble: 8 x 5 #> race n pct cum_n cum_pct #> <dbl> <int> <dbl> <int> <dbl> #> 1 1 368 0.736 368 0.736 #> 2 2 54 0.108 422 0.844 #> 3 3 38 0.076 460 0.92 #> 4 4 13 0.026 473 0.946 #> 5 5 7 0.014 480 0.96 #> 6 6 9 0.018 489 0.978 #> 7 7 8 0.016 497 0.994 #> 8 8 3 0.006 500 1