Skip to contents

Use this function to calculate simple weighted frequencies. You can also specify a grouping variable by which you want to calculate the frequencies.

The x, group, and wt arguments can either be strings or symbols (meaning they can have quotes or no quotes). The benefit of this is that it makes it really easy to iterate this function over a list or vector of variables with other functions like purrr::map() purrr::map() or purrr::walk() purrr::walk() that are found in the purrr package.

Usage

get_freqs(data, x, group, wt, drop_zero = FALSE, decimals = 3, na.rm = TRUE)

Arguments

data

An object of type data.frame or tibble. If piping the data into the function, this is not required.

x

Either a character string or symbol. The variable with which want to get the frequencies.

group

<tidy-select> A selection of columns to group the data by in addition to treats. This operates very similarly to .by from dplyr (for more info on that see ?dplyr_by).

wt

Weights. Add if you have a weighting variable and want to get weighted frequencies.

drop_zero

Logical. Determines if rows with 0 should be removed Default is FALSE.

decimals

Number of decimals each number should be rounded to. Default is 3.

na.rm

Logical. Determines if NAs should be kept or removed Default is TRUE.

Examples

# load the package
library(dplyr)

# Let's calculate the overall frequency for big_events
get_freqs(test_data, big_events)
#> # A tibble: 4 × 3
#>   big_events            n   pct
#>   <fct>             <dbl> <dbl>
#> 1 Strongly agree       45 0.18 
#> 2 Somewhat agree       81 0.324
#> 3 Somewhat disagree    70 0.28 
#> 4 Strongly disagree    54 0.216

# Let's do that again but add weights
get_freqs(test_data, big_events, wt = wts)
#> # A tibble: 4 × 3
#>   big_events            n   pct
#>   <fct>             <dbl> <dbl>
#> 1 Strongly agree     43.3 0.177
#> 2 Somewhat agree     73.6 0.300
#> 3 Somewhat disagree  71.0 0.290
#> 4 Strongly disagree  57.2 0.233

# Can also a grouping variable by specifying the group arg
get_freqs(test_data, big_events, group = pid_f3, wt = wts)
#> # A tibble: 12 × 4
#> # Groups:   pid_f3 [3]
#>    pid_f3      big_events            n   pct
#>    <fct>       <fct>             <dbl> <dbl>
#>  1 Democrat    Strongly agree    22.1  0.195
#>  2 Democrat    Somewhat agree    22.6  0.199
#>  3 Democrat    Somewhat disagree 41.0  0.363
#>  4 Democrat    Strongly disagree 27.4  0.242
#>  5 Independent Strongly agree     3.44 0.174
#>  6 Independent Somewhat agree     9.41 0.476
#>  7 Independent Somewhat disagree  4.17 0.211
#>  8 Independent Strongly disagree  2.76 0.140
#>  9 Republican  Strongly agree    17.7  0.158
#> 10 Republican  Somewhat agree    41.6  0.371
#> 11 Republican  Somewhat disagree 25.8  0.230
#> 12 Republican  Strongly disagree 27.0  0.241

# You can also group the data and do it
test_data %>% 
  group_by(pid_f3) %>% 
  get_freqs(big_events, wt = wts)
#> # A tibble: 12 × 4
#> # Groups:   pid_f3 [3]
#>    pid_f3      big_events            n   pct
#>    <fct>       <fct>             <dbl> <dbl>
#>  1 Democrat    Strongly agree    22.1  0.195
#>  2 Democrat    Somewhat agree    22.6  0.199
#>  3 Democrat    Somewhat disagree 41.0  0.363
#>  4 Democrat    Strongly disagree 27.4  0.242
#>  5 Independent Strongly agree     3.44 0.174
#>  6 Independent Somewhat agree     9.41 0.476
#>  7 Independent Somewhat disagree  4.17 0.211
#>  8 Independent Strongly disagree  2.76 0.140
#>  9 Republican  Strongly agree    17.7  0.158
#> 10 Republican  Somewhat agree    41.6  0.371
#> 11 Republican  Somewhat disagree 25.8  0.230
#> 12 Republican  Strongly disagree 27.0  0.241

# you can also group by two or more variables
get_freqs(test_data, big_events, group = c(pid_f3, edu_f2), wt = wts)
#> # A tibble: 24 × 5
#> # Groups:   pid_f3, edu_f2 [6]
#>    pid_f3      edu_f2                       big_events            n   pct
#>    <fct>       <fct>                        <fct>             <dbl> <dbl>
#>  1 Democrat    No College Degree            Strongly agree    14.1  0.203
#>  2 Democrat    No College Degree            Somewhat agree    16.4  0.236
#>  3 Democrat    No College Degree            Somewhat disagree 27.9  0.401
#>  4 Democrat    No College Degree            Strongly disagree 11.2  0.160
#>  5 Democrat    At Least a Bachelor's Degree Strongly agree     8.01 0.183
#>  6 Democrat    At Least a Bachelor's Degree Somewhat agree     6.16 0.141
#>  7 Democrat    At Least a Bachelor's Degree Somewhat disagree 13.2  0.302
#>  8 Democrat    At Least a Bachelor's Degree Strongly disagree 16.3  0.373
#>  9 Independent No College Degree            Strongly agree     2.57 0.162
#> 10 Independent No College Degree            Somewhat agree     8.42 0.530
#> # ℹ 14 more rows

# also works when the arguments are strings
get_freqs(test_data, "big_events", group = c("pid_f3", "edu_f2"), wt = "wts")
#> # A tibble: 24 × 5
#> # Groups:   pid_f3, edu_f2 [6]
#>    pid_f3      edu_f2                       big_events            n   pct
#>    <fct>       <fct>                        <fct>             <dbl> <dbl>
#>  1 Democrat    No College Degree            Strongly agree    14.1  0.203
#>  2 Democrat    No College Degree            Somewhat agree    16.4  0.236
#>  3 Democrat    No College Degree            Somewhat disagree 27.9  0.401
#>  4 Democrat    No College Degree            Strongly disagree 11.2  0.160
#>  5 Democrat    At Least a Bachelor's Degree Strongly agree     8.01 0.183
#>  6 Democrat    At Least a Bachelor's Degree Somewhat agree     6.16 0.141
#>  7 Democrat    At Least a Bachelor's Degree Somewhat disagree 13.2  0.302
#>  8 Democrat    At Least a Bachelor's Degree Strongly disagree 16.3  0.373
#>  9 Independent No College Degree            Strongly agree     2.57 0.162
#> 10 Independent No College Degree            Somewhat agree     8.42 0.530
#> # ℹ 14 more rows