R/haven_value_label.R

Defines functions haven_value_label

Documented in haven_value_label

#' Create a list of value labels from SPSS or Stata data
#'
#' Create a list of value labels from SPSS or Stata data
#'
#' @importFrom dplyr tibble select
#' @importFrom purrr map pluck attr_getter
#' @importFrom rlang inherits_any
#' @importFrom tidyr unnest
#' @importFrom haven is.labelled
#'
#' @export
#'



haven_value_label <- function(data) {
  if(!inherits_any(data, c('data.frame', 'tbl_df'))) stop('`data` must be `data.frame` or `tibble`')
  else {
    tibble(
      変数 = names(select(data, where(is.labelled))),= map(select(data, where(is.labelled)),
              \(x) pluck(x, attr_getter('labels'))),
      ラベル = map(, names)
    ) %>%
      mutate(= map(, as.character)) %>%
      unnest(cols = c(, ラベル))
  }
}
Sickle-Sword/kamaken documentation built on Feb. 2, 2025, 2:56 a.m.