drop_labels

0th

Percentile

Drop, add or convert (non-)labelled values

For (partially) labelled vectors, zap_labels() will replace all values that have a value label attribute with NA; zap_unlabelled(), as counterpart, will replace all values that don't have a value label attribute with NA.

drop_labels() drops all value labels for unused values, i.e. values that are not present in a vector. fill_labels() is the counterpart to drop_labels() and adds value labels to a partially labelled vector, i.e. if not all values are labelled, non-labelled values get labels.

Usage
drop_labels(x, ..., drop.na = TRUE)

fill_labels(x, ...)

zap_labels(x, ...)

zap_unlabelled(x, ...)

Arguments
x

(partially) labelled vector or a data frame with such vectors.

...

Optional, unquoted names of variables that should be selected for further processing. Required, if x is a data frame (and no vector) and only selected variables from x should be processed. You may also use functions like : or tidyselect's select_helpers. See 'Examples'.

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

  • For zap_labels(), x, where all labelled values are converted to NA.

  • For zap_unlabelled(), x, where all non-labelled values are converted to NA.

  • For drop_labels(), x, where value labels for non-existing values are removed.

  • For fill_labels(), x, where labels for non-labelled values are added.

If x is a data frame, the complete data frame x will be returned, with variables specified in ... being converted; if ... is not specified, applies to all variables in the data frame.

Aliases
  • drop_labels
  • fill_labels
  • zap_labels
  • zap_unlabelled
Examples
# NOT RUN {
# zap_labels() ----

data(efc)
str(efc$e42dep)

x <- set_labels(
  efc$e42dep,
  labels = c("independent" = 1, "severe dependency" = 4)
)
table(x)
get_values(x)
str(x)

# zap all labelled values
table(zap_labels(x))
get_values(zap_labels(x))
str(zap_labels(x))

# zap all unlabelled values
table(zap_unlabelled(x))
get_values(zap_unlabelled(x))
str(zap_unlabelled(x))

# in a pipe-workflow
library(dplyr)
efc %>%
  select(c172code, e42dep) %>%
  set_labels(
    e42dep,
    labels = c("independent" = 1, "severe dependency" = 4)
  ) %>%
  zap_labels()


# drop_labels() ----

library(sjmisc)
rp <- rec_pattern(1, 100)
rp

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


# fill_labels() ----

# create labelled integer, with tagged missings
library(haven)
x <- labelled(
  c(1:3, tagged_na("a", "c", "z"), 4:1),
  c("Agreement" = 1, "Disagreement" = 4, "First" = tagged_na("c"),
    "Refused" = tagged_na("a"), "Not home" = tagged_na("z"))
  )
# get current values and labels
x
get_labels(x)

fill_labels(x)
get_labels(fill_labels(x))
# same as
get_labels(x, non.labelled = TRUE)

# }
Documentation reproduced from package sjlabelled, version 1.0.14, License: GPL-3

Community examples

Looks like there are no examples yet.