@@ -262,63 +262,55 @@ zap_attributes.data.frame <- function(x, attributes = NULL) {
262262# ' Modelled on [haven::zap_labels()], zaps variable labels (not value labels).
263263# '
264264# ' @param x the data frame or variable
265+ # '
265266# ' @export
267+ # ' @examples
268+ # ' x <- haven::labelled(rep(1:5, each = 1), c(Bad = 1, Good = 5))
269+ # ' zap_label(x)
266270zap_label <- function (x ) {
267271 UseMethod(" zap_label" )
268272}
273+
274+ # ' @export
269275zap_label.data.frame <- function (x ) {
270276 x [] <- lapply(x , zap_label )
271277 x
272278}
279+
280+ # ' @export
273281zap_label.default <- function (x ) {
274282 attr(x , " label" ) <- NULL
275283 x
276284}
277285
278286
279-
280- # ' Aggregate variables and remember which variables this were
287+ # ' Zap labelled class
281288# '
282- # ' The resulting variables will have the attribute `scale_item_names` containing
283- # ' the basis for aggregation. Its `label` attribute will refer to the common stem of the
284- # ' aggregated variable names (if any), the number of variables, and the
285- # ' aggregation function.
289+ # ' Modelled on [haven::zap_labels()], zaps labelled class (not other attributes).
286290# '
287- # ' @param items data.frame of the items that should be aggregated
288- # ' @param fun aggregation function, defaults to rowMeans with na.rm = FALSE
289- # ' @param stem common stem for the variables, specify if it should not be auto-detected
290- # ' as the longest common stem of the variable names
291+ # ' @param x the data frame or variable
291292# ' @export
292- # ' @examples
293- # ' testdf <- data.frame(bfi_neuro_1 = rnorm(20), bfi_neuro_2 = rnorm(20),
294- # ' bfi_neuro_3R = rnorm(20), age = rpois(20, 30))
295- # ' item_names <- c('bfi_neuro_1', 'bfi_neuro_2', 'bfi_neuro_3R')
296- # ' testdf$bfi_neuro <- aggregate_and_document_scale(testdf[, item_names])
297- # ' testdf$bfi_neuro
298- aggregate_and_document_scale <- function (items , fun = rowMeans , stem = NULL ) {
299- new_scale <- fun(items )
300- item_names <- names(items )
301- attributes(new_scale )$ scale_item_names <- item_names
293+ zap_labelled <- function (x ) {
294+ UseMethod(" zap_labelled" )
295+ }
302296
303- # find longest common stem
304- if (is.null(stem )) {
305- max_len <- min(nchar(item_names ))
306- for (l in max_len : 0 ) {
307- stem <- unique(stringr :: str_sub(item_names , 1 , l ))
308- if (length(stem ) == 1 ) break
309- }
310- }
311- # string trimming for idiots
312- if (nchar(stem )) {
313- stem <- stringr :: str_match(stem , " ^(.+?)_?$" )[, 2 ]
314- }
297+ # ' @export
298+ zap_labelled.data.frame <- function (x ) {
299+ x [] <- lapply(x , zap_labelled )
300+ x
301+ }
315302
316- attributes(new_scale )$ label <- paste(ncol(items ), stem , " items aggregated by" ,
317- deparse(substitute(fun )))
318- new_scale
303+ # ' @export
304+ zap_labelled.labelled <- function (x ) {
305+ if (inherits(x , " labelled" )) {
306+ unclass(x )
307+ } else {
308+ x
309+ }
319310}
320311
321312
313+
322314# ' Reverse labelled values
323315# ' reverse the underlying valus for a numeric [haven::labelled()] vector while keeping the labels correct
324316# '
@@ -333,6 +325,14 @@ reverse_labelled_values <- function(x) {
333325 labels <- attributes(x )$ labels
334326 values <- unname(labels )
335327 labels <- names(labels )
328+ if (
329+ sum(! is.na(values )) == 0 ||
330+ (any(x > max(values , na.rm = TRUE ) |
331+ x < min(values , na.rm = TRUE ), na.rm = TRUE ))) {
332+ warning(deparse(substitute(x )), " : There are values outside the " ,
333+ " labelled range. Reversion will only work if both the minimum " ,
334+ " and maximum of the range are part of the responses." )
335+ }
336336 if (length(values ) < length(unique(x )) ) {
337337 # if only some values have labels (e.g. extremes), make sure we include all
338338 possible_replies <- union(values , unique(x ))
@@ -349,6 +349,7 @@ reverse_labelled_values <- function(x) {
349349 length(range )) {
350350 possible_replies <- range
351351 }
352+
352353 possible_replies <- sort(possible_replies )
353354 recode_replies <- stats :: setNames(
354355 as.list(possible_replies ), rev(possible_replies ))
0 commit comments