R/haven_helpers.R

Defines functions .concat_value_label .get_label get_variable_labels get_value_labels get_overview

Documented in get_overview get_value_labels get_variable_labels

# Internal functions -----------------------------------------------------------

# Concat value labels (value 1 = value label 1, value 2 = value label 2, ...)
.concat_value_label <- function(x) {
  lbl <- paste(x,
               " = ",
               names(x),
               collapse = ", ")
  return(lbl)
}

# Reads the attribute (value label or variable label)
.get_label <- function(x, value_lbl = FALSE, truncate = 0) {
  # Check which type of label is wanted
  if (value_lbl) {
    type <- "labels" # haven's attribute for value labels
  } else {
    type <- "label" # haven's attribute vor variable labels
  }
  # Read labels and check for missings
  lbls <- sapply(x, attr, which = type, exact = TRUE)
  no_lbls <- sapply(lbls, is.null)
  if (all(no_lbls)) {
    warning("No labels found")
  }
  # Format value labels
  if (value_lbl) {
    lbls <- sapply(lbls, .concat_value_label)
  }
  # Truncate at specified number of chars
  if (truncate > 0) {
    lbls <- strtrim(lbls, truncate)
  }
  # Set missing labels to NA and return labels
  lbls[no_lbls] <- NA
  return(lbls)
}

# Main funtions ----------------------------------------------------------------

#' Get a list of all variable labels in a data set
#'
#' get_variable_labels returns the variable labels generated by data import
#' functions from the haven package.
#'
#' @param x A tibble read in by the haven package.
#' @param truncate The maximum number of characters for each label. Defaults to
#' 0 for no truncation.
#'
#' @return A named list of all variable labels.
#' @export
get_variable_labels <- function(x, truncate = 0) {
  var_labels <- .get_label(x = x, value_lbl = FALSE, truncate = truncate)
  return(var_labels)
}

#' Get a list of all value labels in a data set
#'
#' get_value_labels returns the value labels generated by data import
#' functions from the haven package.
#'
#' @param x A tibble read in by the haven package.
#' @param truncate The maximum number of characters for each label. Defaults to
#' 0 for no truncation.
#'
#' @return A named list of all value labels.
#' @export
get_value_labels <- function(x, truncate = 0) {
  val_labels <- .get_label(x = x, value_lbl = TRUE, truncate = truncate)
  return(val_labels)
}

#' Overview over all variables and their labels in a data set
#'
#' get_overview returns a data.frame with variable names, variable labels, and
#' value labels. It should be used with data imported by the haven
#' package.
#'
#' @param x A tibble (read in by the haven package).
#' @param trunc_vallab The maximum number of characters for value labels.
#' Defaults to 0 for no truncation.
#' @param trunc_varlab The maximum number of characters for variable labels.
#' Defaults to 0 for no truncation.
#'
#' @return A data frame with variable names, variable labels, and
#' value labels.
#' @export
get_overview <- function(x, trunc_varlab = 0, trunc_vallab = 0) {
  overview <- cbind(var = names(x),
                    varlab = get_variable_labels(x, truncate = trunc_varlab),
                    vallab = get_value_labels(x, truncate = trunc_vallab))
  return(data.frame(overview))
}
jogrue/jogRu documentation built on Nov. 10, 2019, 9:13 a.m.