Learn R Programming

sjmisc (version 2.0.1)

drop_labels: Drop labels of zero-count values

Description

This function drops all value labels for unused values that have no cases (frequencies) in a vector.

Usage

drop_labels(x, drop.na = TRUE)

Arguments

x
Variable (vector), data.frame or list of variables with partially added value labels (see labelled).
drop.na
Logical, whether existing value labels of tagged NA values (see tagged_na) should be removed (drop.na = TRUE, the default) or preserved (drop.na = FALSE). See get_na for more details on tagged NA values.

Value

x, where value labels for non-existing values are removed.

See Also

zap_labels and zap_unlabelled to convert (non-)labelled values into NA; fill_labels to add labels to existing, but not yet labelled values. The latter function is the counterpart to drop_labels.

Examples

Run this code
rp <- rec_pattern(1, 100)
rp

# sample data
data(efc)
# recode carers age into groups of width 5
x <- rec(efc$c160age, rp$pattern)
# add value labels to new vector
set_labels(x) <- rp$labels

# watch result. due to recode-pattern, we have age groups with
# no observations (zero-counts)
frq(x)
# now, let's drop zero's
frq(drop_labels(x))

# drop labels, also drop NA value labels, then also zap tagged NA
library(haven)
x <- labelled(c(1:3, tagged_na("z"), 4:1),
              c("Agreement" = 1, "Disagreement" = 4, "Unused" = 5,
                "Not home" = tagged_na("z")))
x
drop_labels(x, drop.na = FALSE)
drop_labels(x)
zap_na_tags(drop_labels(x))

Run the code above in your browser using DataLab