R/init.R

Defines functions .init.i_labelled .init.default .init

Documented in .init

#' backend for i_labelled
#' @description
#' all arguments are passed from i_labelled
#'
#' @returns x as i_labelled object with attributes applied to it
#' @param x vector
#' @param label variable label
#' @param labels value labels as named vector
#' @param na_values missing values (e.g. c(888, 999))
#' @param na_range range of missing values (e.g. c(-9,-1))
#' @param scale scale level (nominal, ordinal, scale)
#' @param annotation additional information about variable
#' @param wording question text
#' @param subject subject
#' @param ... further attributes passed to class
.init <- function(x, label = NULL, labels = NULL, na_values = NULL, na_range = NULL, scale = NULL, annotation = NULL, wording = NULL, subject = NULL, ...) {
  UseMethod(".init")
}


.init.default <- function(x, label = NULL, labels = NULL, na_values = NULL, na_range = NULL, scale = NULL, annotation = NULL, wording = NULL, subject = NULL, ...) {
  # set class i_labelled
  if(is.numeric(x) || is.factor(x) || is.logical(x)){
    structure(
      .Data = as.double(x),
      class = c("i_labelled", "double"),
      label = label,
      labels = labels,
      na_values = na_values,
      na_range = na_range,
      scale = scale,
      annotation = annotation,
      wording = wording,
      subject = subject,
      ...
    )
  }else{
    structure(
      .Data = as.character(x),
      class = c("i_labelled", "character"),
      label = label,
      labels = labels,
      na_values = na_values,
      na_range = na_range,
      scale = scale,
      annotation = annotation,
      wording = wording,
      subject = subject,
      ...
    )
  }
}


.init.i_labelled <- function(x, label = NULL, labels = NULL, na_values = NULL, na_range = NULL, scale = NULL, annotation = NULL, wording = NULL, subject = NULL, ...) {
  # do nothing
  x
}

Try the ilabelled package in your browser

Any scripts or data that you put into this service are public.

ilabelled documentation built on April 3, 2025, 10:38 p.m.