Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

characterize

Character conversion of labelled data


Description

Convert labelled variables to character or factor

Usage

characterize(x, ...)

factorize(x, ...)

## Default S3 method:
characterize(x, ...)

## S3 method for class 'data.frame'
characterize(x, ...)

## Default S3 method:
factorize(x, coerce_character = FALSE, ...)

## S3 method for class 'data.frame'
factorize(x, ...)

Arguments

x

A vector or data frame.

...

additional arguments passed to methods

coerce_character

A logical indicating whether to additionally coerce character columns to factor (in factorize). Default FALSE.

Details

characterize converts a vector with a labels attribute of named levels into a character vector. factorize does the same but to factors. This can be useful at two stages of a data workflow: (1) importing labelled data from metadata-rich file formats (e.g., Stata or SPSS), and (2) exporting such data to plain text files (e.g., CSV) in a way that preserves information.

See Also

Examples

# vector method
x <- structure(1:4, labels = c("A" = 1, "B" = 2, "C" = 3))
characterize(x)
factorize(x)

# data frame method
x <- data.frame(v1 = structure(1:4, labels = c("A" = 1, "B" = 2, "C" = 3)),
                v2 = structure(c(1,0,0,1), labels = c("foo" = 0, "bar" = 1)))
str(factorize(x))
str(characterize(x))

# comparison of exported file contents
import(export(x, "example.csv"))
import(export(factorize(x), "example.csv"))

# cleanup
unlink("example.csv")

rio

A Swiss-Army Knife for Data I/O

v0.5.26
GPL-2
Authors
Jason Becker [ctb], Chung-hong Chan [aut], Geoffrey CH Chan [ctb], Thomas J. Leeper [aut, cre] (<https://orcid.org/0000-0003-4097-6326>), Christopher Gandrud [ctb], Andrew MacDonald [ctb], Ista Zahn [ctb], Stanislaus Stadlmann [ctb], Ruaridh Williamson [ctb], Patrick Kennedy [ctb], Ryan Price [ctb], Trevor L Davis [ctb], Nathan Day [ctb], Bill Denney [ctb] (<https://orcid.org/0000-0002-5759-428X>), Alex Bokov [ctb] (<https://orcid.org/0000-0002-0511-9815>)
Initial release
2021-02-22

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.