Learn R Programming

mariposa (version 0.7.3)

to_character: Convert Labelled Variables to Character

Description

Converts haven_labelled variables to character vectors using value labels as text. Same as to_label() but returns character instead of factor.

Usage

to_character(
  data,
  ...,
  drop_na = TRUE,
  add_non_labelled = FALSE,
  drop.na = NULL,
  add.non.labelled = NULL
)

Value

The input with labelled variables converted to character. For single vector input, returns a character vector.

Arguments

data

A data frame, tibble, or a single vector.

...

Optional: unquoted variable names (tidyselect supported). If empty, converts all haven_labelled columns.

drop_na

If TRUE (default), tagged NAs become regular NA.

add_non_labelled

If TRUE, unlabelled values are included as their numeric string representation. Default: FALSE.

drop.na, add.non.labelled

Defunct dot-case argument names, removed in mariposa 0.6.9. Calling the function with any of them is an error; use the snake_case equivalents instead. (The formals are retained only so that the old names error clearly instead of being swallowed by ....)

Details

This is a convenience wrapper around to_label() that calls as.character() on the result. Use this when you need character output (e.g., for string operations) rather than factor output.

The variable label ("label" attribute) is preserved on the output.

See Also

to_label() for factor output, val_labels()

Other labels: copy_labels(), drop_labels(), find_var(), set_na(), to_label(), to_labelled(), to_numeric(), unlabel(), val_labels(), var_label()

Examples

Run this code
# Convert a single variable to character
to_character(survey_data$gender)

# Convert specific columns in a data frame
data <- to_character(survey_data, gender, region)

Run the code above in your browser using DataLab