R/remove_attributes.R

Defines functions remove_attributes.data.frame remove_attributes.default remove_attributes

Documented in remove_attributes

#' Remove attributes
#'
#' This function removes specified attributes. When applied to a data.frame, it
#' will also remove recursively the specified attributes to each column of the
#' data.frame.
#'
#' @param x an object
#' @param attributes a character vector indicating attributes to remove
#' @export
#' @examples
#' \dontrun{
#' library(haven)
#' path <- system.file("examples", "iris.sav", package = "haven")
#' d <- read_sav(path)
#' str(d)
#' d <- remove_attributes(d, "format.spss")
#' str(d)
#' }
remove_attributes <- function(x, attributes) {
  UseMethod("remove_attributes")
}

#' @export
remove_attributes.default <- function(x, attributes) {
  for (a in attributes) attr(x, a) <- NULL
  x
}

#' @export
remove_attributes.data.frame <- function(x, attributes) {
  cl <- class(x)
  x <- remove_attributes.default(x, attributes)
  x <- dplyr::as_tibble(
    lapply(x, remove_attributes, attributes = attributes)
  )
  class(x) <- cl
  x
}
larmarange/labelled documentation built on Oct. 11, 2024, 6:25 p.m.