Use this function to calculate simple weighted frequencies weighted grouped. You can also specify a grouping variable by which you want to calculate the frequencies
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
Either a character string or a symbol. The grouping variable.
- wt
Weights. Add if you have a weighting variable and want to get weighted frequencies
- cross_tab
Logical. If a
group
object has been supplied, should the the table be pivoted to create make it like crosstabs
Details
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 map()
purrr::map()
or walk()
purrr::walk()
that are found in the purrr
package.
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
#> * <dbl+lbl> <int> <dbl>
#> 1 1 [Strongly agree] 45 0.18
#> 2 2 [Somewhat agree] 81 0.324
#> 3 3 [Somewhat disagree] 70 0.28
#> 4 4 [Strongly disagree] 54 0.216
# it also works if x is a string
get_means(test_data, "big_events")
#> # A tibble: 1 × 5
#> mean sd n conf.low conf.high
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2.53 1.02 250 2.4 2.66
# Let's do that again but add weights
get_freqs(test_data, big_events, wt = wts)
#> # A tibble: 4 × 3
#> big_events n pct
#> * <dbl+lbl> <dbl> <dbl>
#> 1 1 [Strongly agree] 43.3 0.177
#> 2 2 [Somewhat agree] 73.6 0.300
#> 3 3 [Somewhat disagree] 71 0.290
#> 4 4 [Strongly disagree] 57.2 0.233
# the wt argument can also be in quotes like this
get_freqs(test_data, "big_events", wt = "wts")
#> # A tibble: 4 × 3
#> big_events n pct
#> * <dbl+lbl> <dbl> <dbl>
#> 1 1 [Strongly agree] 43.3 0.177
#> 2 2 [Somewhat agree] 73.6 0.300
#> 3 3 [Somewhat disagree] 71 0.290
#> 4 4 [Strongly disagree] 57.2 0.233
# Now let's do the average score for different education levels
get_freqs(test_data, big_events, edu_f, wts)
#> # A tibble: 16 × 4
#> # Groups: edu_f [4]
#> edu_f big_events n pct
#> * <fct> <dbl+lbl> <dbl> <dbl>
#> 1 High School or Less 1 [Strongly agree] 12.8 0.183
#> 2 High School or Less 2 [Somewhat agree] 20.7 0.296
#> 3 High School or Less 3 [Somewhat disagree] 22.2 0.319
#> 4 High School or Less 4 [Strongly disagree] 14.1 0.202
#> 5 Some College 1 [Strongly agree] 17.7 0.205
#> 6 Some College 2 [Somewhat agree] 29.2 0.338
#> 7 Some College 3 [Somewhat disagree] 22 0.255
#> 8 Some College 4 [Strongly disagree] 17.4 0.202
#> 9 Bachelor's Degree 1 [Strongly agree] 6.9 0.139
#> 10 Bachelor's Degree 2 [Somewhat agree] 13.3 0.268
#> 11 Bachelor's Degree 3 [Somewhat disagree] 16.3 0.329
#> 12 Bachelor's Degree 4 [Strongly disagree] 13.1 0.264
#> 13 Graduate Degree 1 [Strongly agree] 5.9 0.149
#> 14 Graduate Degree 2 [Somewhat agree] 10.4 0.265
#> 15 Graduate Degree 3 [Somewhat disagree] 10.5 0.265
#> 16 Graduate Degree 4 [Strongly disagree] 12.6 0.321
# it also works with quotes
get_freqs(test_data, "big_events", "edu_f", "wts")
#> # A tibble: 16 × 4
#> # Groups: edu_f [4]
#> edu_f big_events n pct
#> * <fct> <dbl+lbl> <dbl> <dbl>
#> 1 High School or Less 1 [Strongly agree] 12.8 0.183
#> 2 High School or Less 2 [Somewhat agree] 20.7 0.296
#> 3 High School or Less 3 [Somewhat disagree] 22.2 0.319
#> 4 High School or Less 4 [Strongly disagree] 14.1 0.202
#> 5 Some College 1 [Strongly agree] 17.7 0.205
#> 6 Some College 2 [Somewhat agree] 29.2 0.338
#> 7 Some College 3 [Somewhat disagree] 22 0.255
#> 8 Some College 4 [Strongly disagree] 17.4 0.202
#> 9 Bachelor's Degree 1 [Strongly agree] 6.9 0.139
#> 10 Bachelor's Degree 2 [Somewhat agree] 13.3 0.268
#> 11 Bachelor's Degree 3 [Somewhat disagree] 16.3 0.329
#> 12 Bachelor's Degree 4 [Strongly disagree] 13.1 0.264
#> 13 Graduate Degree 1 [Strongly agree] 5.9 0.149
#> 14 Graduate Degree 2 [Somewhat agree] 10.4 0.265
#> 15 Graduate Degree 3 [Somewhat disagree] 10.5 0.265
#> 16 Graduate Degree 4 [Strongly disagree] 12.6 0.321
# if we want to pivot the results so they look like cross tabs, then we need
# to set `cross_tab` to TRUE
get_freqs(test_data, big_events, edu_f, wts, cross_tab = TRUE)
#> # A tibble: 4 × 5
#> big_events `High School or Less` `Some College` `Bachelor's Degree`
#> * <dbl+lbl> <glue> <glue> <glue>
#> 1 1 [Strongly agree] 18.35% (n = 12.8) 20.5% (n = 17… 13.94% (n = 6.9)
#> 2 2 [Somewhat agree] 29.64% (n = 20.7) 33.83% (n = 2… 26.76% (n = 13.3)
#> 3 3 [Somewhat disagree] 31.85% (n = 22.2) 25.5% (n = 22) 32.89% (n = 16.3)
#> 4 4 [Strongly disagree] 20.15% (n = 14.1) 20.17% (n = 1… 26.41% (n = 13.1)
#> # ℹ 1 more variable: `Graduate Degree` <glue>
# you can also pipe in the `data` argument if you want to do some data
# transformations before you calculate the means. For example, say you want
# to compare the frequencies of `big_events` among people who agreed vs
# disagreed with the variable `top`:
test_data %>%
mutate(top_f2 = make_dicho(top)) %>%
get_freqs(trad_n, top_f2, wts)
#> # A tibble: 30 × 4
#> # Groups: top_f2 [2]
#> top_f2 trad_n n pct
#> * <fct> <dbl> <dbl> <dbl>
#> 1 Agree 0 7.9 0.0713
#> 2 Agree 1 13.9 0.125
#> 3 Agree 2 9.5 0.0856
#> 4 Agree 3 20.2 0.181
#> 5 Agree 4 11.6 0.104
#> 6 Agree 5 6.7 0.0603
#> 7 Agree 6 2.9 0.0260
#> 8 Agree 7 6.3 0.0562
#> 9 Agree 8 9.1 0.0814
#> 10 Agree 9 3.1 0.0280
#> # ℹ 20 more rows