Commit
…384) The constructor functions `labelled()` and `labelled_spss()` now support adding the `label` attribute to the resulting object. The `label` is a short, human-readable description of the object, and is now also used when printing, and can be easily removed using the new `zap_label()` function. Fixes #362. Previously, the `label` attribute was supported both when reading and writing SPSS files, but it was not possible to actually create objects in R having the `label` attribute using the constructors `labelled()` or `labelled_spss()`.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#' Zap label | ||
#' | ||
#' Removes label, leaving unlabelled vectors as is. Use this if you want to | ||
#' simply drop all `label` attributes from a data frame. | ||
#' | ||
#' @param x A vector or data frame | ||
#' @family zappers | ||
#' @export | ||
#' @examples | ||
#' x1 <- labelled(1:5, c(good = 1, bad = 5)) | ||
#' x1 | ||
#' zap_label(x1) | ||
#' | ||
#' x2 <- labelled_spss(c(1:4, 9), c(good = 1, bad = 5), na_values = 9) | ||
#' x2 | ||
#' zap_label(x2) | ||
#' | ||
#' # zap_label also works with data frames | ||
#' df <- tibble::data_frame(x1, x2) | ||
#' df | ||
#' zap_label(df) | ||
zap_label <- function(x) { | ||
UseMethod("zap_label") | ||
} | ||
|
||
#' @export | ||
zap_label.default <- function(x) { | ||
x | ||
} | ||
|
||
#' @export | ||
zap_label.labelled <- function(x) { | ||
attr(x, "label") <- NULL | ||
x | ||
} | ||
|
||
#' @export | ||
zap_label.data.frame <- function(x) { | ||
x[] <- lapply(x, zap_label) | ||
x | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.