Skip to contents

This function adds composites (item means by default). The Cronbach's alpha is also printed to the console.

Usage

add_composites(
  df,
  map,
  fn = rowMeans,
  na.rm = TRUE,
  standardize_items = FALSE,
  check_already_exist = FALSE,
  ...
)

Arguments

df

data.frame

map

named list where names are new variable names, elements are character vectors with variable names to use

fn

function to apply to each row (default base::rowMeans())

na.rm

(logical) remove missing (NA) before computing composite? (default TRUE)

standardize_items

(logical) standardize items before computing composite? (default FALSE)

check_already_exist

(logical) stop if variable already exists? (default FALSE)

...

other arguments passed to fn()

Value

df with added composite variables

Details

Function in fn must take na.rm as an argument, and return a vector with a value for each row in df. Good candidate functions are base::rowMeans() and base::rowSums(). Users can also supply custom functions.

Examples

comp <- list(X=c("x1","x2","x3"))
df <- df |> add_composites(comp)
#> Number of categories should be increased  in order to count frequencies. 
#>  raw_alpha std.alpha
#>  0.8476492  0.870315
#> [1] "Added vars:"
#> [1] "X"