R/score_from_dic.R

Defines functions score_from_dic

Documented in score_from_dic

#' Automatic scale scoring from a dic file
#'
#' @param data A data frame with dic information
#' @param dic A dic file with scoring information
#'
#' @return A data frame with added scale scores
#' @export

score_from_dic <- function(data, dic, check_values = TRUE, impute_values = FALSE) {

  dic <- dic[!is.na(dic[[.opt$score_filter]]), ]

  if (nrow(dic) == 0) return(data)

  if (!.opt$score_function %in% names(dic)) dic[[.opt$score_function]] <- "mean"

  for(i in 1:nrow(dic)) {
    if (dic[[.opt$score_function]][i] == "mean") sum <- FALSE else sum <- TRUE
    new_var <- dic[[.opt$item_name]][i]
    filter <- dic[[.opt$score_filter]][i]
    label <- dic[[.opt$item_label]][i]

    if (impute_values) {
      data <- .impute_missing(data, filter)
    }

    data[[new_var]] <- .score_scale(data, filter = filter, label = label, FUN = NULL, sum = sum, bind = FALSE)

  }

  data

}
jazznbass/scaledic documentation built on Sept. 20, 2021, 7:43 p.m.