tally_scale: Tally scale

View source: R/tally_scale.R

tally_scaleR Documentation

Tally scale

Description

This function aggregates responses from a set of columns into a single score which gets appended as a new column, e.g. sum or mean score

Usage

tally_scale(
  df,
  var_regex,
  new_var_name = NULL,
  join_function = dplyr::full_join,
  na.rm = F,
  tally_function = sum
)

Arguments

df

Dataframe of data

var_regex

Regex string to capture column names which will be aggregated

new_var_name

Optional argument defining the column name for the aggregate score. If no value is supplied, the new column will be named the value of var_regex

join_function

Optional argument specifying join function. If NULL, aggregated values will be returned without original data

na.rm

Optional argument passed to tally_function

tally_function

Function used to tally scores, e.g. mean, sum, etc.

Value

A dataframe with tallyed scales appended or dataframe with tallyed scales alone

Examples


library(dplyr)
df <- tribble(
~scale_sub1_1, ~scale_subscale1_2, ~scale_subscale2_1, ~scale_subscale2_2,
1,             2,                  3,                  3,
2,             3,                  2,                  3,
3,             2,                  3,                  3
)
df %>%
   tally_scale("^scale_(subscale1|subscale2)_[0-9]$", "scale_total") %>%
   tally_scale("^scale_subscale1_[0-9]$", "scale_subscale1_total") %>%
   tally_scale("^scale_subscale2_[0-9]$", "scale_subscale2_total")


amehtaSF/rsurveyutils documentation built on Aug. 27, 2022, 11:59 a.m.