[go: up one dir, main page]

File: characterize.Rd

package info (click to toggle)
r-cran-rio 1.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,052 kB
  • sloc: makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,917 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/characterize.R
\name{characterize}
\alias{characterize}
\alias{factorize}
\alias{characterize.default}
\alias{characterize.data.frame}
\alias{factorize.default}
\alias{factorize.data.frame}
\title{Character conversion of labelled data}
\usage{
characterize(x, ...)

factorize(x, ...)

\method{characterize}{default}(x, ...)

\method{characterize}{data.frame}(x, ...)

\method{factorize}{default}(x, coerce_character = FALSE, ...)

\method{factorize}{data.frame}(x, ...)
}
\arguments{
\item{x}{A vector or data frame.}

\item{\dots}{additional arguments passed to methods}

\item{coerce_character}{A logical indicating whether to additionally coerce character columns to factor (in \code{factorize}). Default \code{FALSE}.}
}
\value{
a character vector (for \code{characterize}) or factor vector (for \code{factorize})
}
\description{
Convert labelled variables to character or factor
}
\details{
\code{characterize} converts a vector with a \code{labels} attribute of named levels into a character vector. \code{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.
}
\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))

## Application
csv_file <- tempfile(fileext = ".csv")
## comparison of exported file contents
import(export(x, csv_file))
import(export(factorize(x), csv_file))
}
\seealso{
\code{\link[=gather_attrs]{gather_attrs()}}
}