Learn R Programming

sjmisc (version 2.8.4)

to_character: Convert variable into character vector and replace values with associated value labels

Description

This function converts (replaces) variable values (also of factors or character vectors) with their associated value labels and returns them as character vector. This is just a convenient wrapper for as.character(to_label(x)).

Usage

to_character(
  x,
  ...,
  add.non.labelled = FALSE,
  prefix = FALSE,
  var.label = NULL,
  drop.na = TRUE,
  drop.levels = FALSE
)

Arguments

x

A vector or data frame.

...

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' or package-vignette.

add.non.labelled

Logical, if TRUE, values without associated value label will also be converted to labels (as is). See 'Examples'.

prefix

Logical, if TRUE, the value labels used as factor levels or character values will be prefixed with their associated values. See 'Examples'.

var.label

Optional string, to set variable label attribute for the returned variable (see vignette Labelled Data and the sjlabelled-Package). If NULL (default), variable label attribute of x will be used (if present). If empty, variable label attributes will be removed.

drop.na

Logical, if TRUE, tagged NA values with value labels will be converted to regular NA's. Else, tagged NA values will be replaced with their value labels. See 'Examples' and get_na.

drop.levels

Logical, if TRUE, unused factor levels will be dropped (i.e. droplevels will be applied before returning the result).

Value

A character vector with the associated value labels as values. If x is a data frame, the complete data frame x will be returned, where variables specified in ... are coerced to character variables; if ... is not specified, applies to all variables in the data frame.

Examples

Run this code
# NOT RUN {
library(sjlabelled)
data(efc)
print(get_labels(efc)['c161sex'])
head(efc$c161sex)
head(to_character(efc$c161sex))

# Find more examples at '?sjlabelled::as_label'

# }

Run the code above in your browser using DataLab