Skip to contents

Use this function to calculate simple weighted means with 95% confidence intervals or weighted grouped means.

Usage

get_means(data, x, group, wt, decimals = 2)

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 you want to get the mean.

group

Either a character string or a symbol. The variable you want the means to be grouped by.

wt

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

decimals

Number of decimals to round the results to. Default is 2.

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 average score for trad_n
get_means(test_data, trad_n)
#> # A tibble: 1 × 5
#>    mean    sd     n conf.low conf.high
#>   <dbl> <dbl> <dbl>    <dbl>     <dbl>
#> 1  4.22  3.88   250     3.74       4.7

# it also works if x is a string
get_means(test_data, "trad_n")
#> # A tibble: 1 × 5
#>    mean    sd     n conf.low conf.high
#>   <dbl> <dbl> <dbl>    <dbl>     <dbl>
#> 1  4.22  3.88   250     3.74       4.7

# Let's do that again but add weights
get_means(test_data, trad_n, wt = wts)
#> # A tibble: 1 × 5
#>    mean    sd     n conf.low conf.high
#>   <dbl> <dbl> <dbl>    <dbl>     <dbl>
#> 1  3.97  3.88   250     3.49      4.46

# the wt argument can also be in quotes like this
get_means(test_data, "trad_n", wt = "wts")
#> # A tibble: 1 × 5
#>    mean    sd     n conf.low conf.high
#>   <dbl> <dbl> <dbl>    <dbl>     <dbl>
#> 1  3.97  3.88   250     3.49      4.46

# Now let's do the average score for different education levels
get_means(test_data, trad_n, edu_f, wts)
#> # A tibble: 4 × 6
#>   edu_f                mean    sd     n conf.low conf.high
#>   <fct>               <dbl> <dbl> <dbl>    <dbl>     <dbl>
#> 1 High School or Less  4.14  3.91    64     3.17      5.12
#> 2 Some College         3.93  4.1     78     3         4.85
#> 3 Bachelor's Degree    4.23  3.82    68     3.31      5.16
#> 4 Graduate Degree      3.45  3.49    40     2.33      4.56

# it also works with quotes
get_means(test_data, "trad_n", "edu_f", "wts")
#> # A tibble: 4 × 6
#>   edu_f                mean    sd     n conf.low conf.high
#>   <fct>               <dbl> <dbl> <dbl>    <dbl>     <dbl>
#> 1 High School or Less  4.14  3.91    64     3.17      5.12
#> 2 Some College         3.93  4.1     78     3         4.85
#> 3 Bachelor's Degree    4.23  3.82    68     3.31      5.16
#> 4 Graduate Degree      3.45  3.49    40     2.33      4.56

# 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 means of `trad_n` among people who agreed vs disagreed with
# the variable `top`:
test_data %>%
  mutate(top_f2 = make_dicho(top)) %>%
  get_means(trad_n, top_f2, wts)
#> # A tibble: 2 × 6
#>   top_f2    mean    sd     n conf.low conf.high
#>   <fct>    <dbl> <dbl> <dbl>    <dbl>     <dbl>
#> 1 Agree     5.08  3.84   110     4.36      5.81
#> 2 Disagree  3.05  3.68   140     2.44      3.67