to_factor: Convert variable into factor and keep value labels

Description Usage Arguments Details Value Note See Also Examples

Description

This function converts a variable into a factor, but preserves variable and value label attributes. See 'Examples'.

Usage

1
to_factor(x, ..., add.non.labelled = FALSE, ref.lvl = NULL)

Arguments

x

A vector or data frame.

...

Optional, unquoted names of variables. 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 dplyr's select_helpers. The latter must be stated as formula (i.e. beginning with ~). See 'Examples' or package-vignette.

add.non.labelled

Logical, if TRUE, non-labelled values also get value labels.

ref.lvl

Numeric, specifies the reference level for the new factor. Use this parameter if a different factor level than the lowest value should be used as reference level. If NULL, lowest value will become the reference level. See ref_lvl for details.

Details

to_factor converts numeric values into a factor with numeric levels. to_label, however, converts a vector into a factor and uses value labels as factor levels. Furthermore, see 'Details' in get_na.

Value

A factor variable, including variable and value labels. If x is a data frame, the complete data frame x will be returned, where variables specified in ... are coerced to factor variables (including variable and value labels); if ... is not specified, applies to all variables in the data frame.

Note

This function is intended for use with vectors that have value and variable label attributes. Unlike as.factor, to_factor converts a variable into a factor and preserves the value and variable label attributes.

Adding label attributes is automatically done by importing data sets with one of the read_*-functions, like read_spss. Else, value and variable labels can be manually added to vectors with set_labels and set_label.

See Also

to_value to convert a factor into a numeric value and to_label to convert a value into a factor with labelled factor levels.

Examples

 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
data(efc)
# normal factor conversion, loses value attributes
x <- as.factor(efc$e42dep)
frq(x)

# factor conversion, which keeps value attributes
x <- to_factor(efc$e42dep)
frq(x)

# create parially labelled vector
x <- set_labels(efc$e42dep,
                labels = c(`1` = "independent", `4` = "severe dependency",
                           `9` = "missing value"))

# only copy existing value labels
to_factor(x)
get_labels(to_factor(x), include.values = "p")

# also add labels to non-labelled values
to_factor(x, add.non.labelled = TRUE)
get_labels(to_factor(x, add.non.labelled = TRUE), include.values = "p")


# Convert to factor, using different reference level
x <- to_factor(efc$e42dep)
str(x)
table(x)

x <- to_factor(efc$e42dep, ref.lvl = 3)
str(x)
table(x)


# easily coerce specific variables in a data frame to factor
# and keep other variables, with their class preserved
to_factor(efc, e42dep, e16sex, c172code)

# use select-helpers from dplyr-package
library(dplyr)
to_factor(efc, ~contains("cop"), c161sex:c175empl)


Search within the sjmisc package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.