Convert variable into factor and replace values with associated value labels

Share:

Description

This function converts (replaces) variable values (also of factors or character vectors) with their associated value labels. Might be helpful for factor variables. For instance, if you have a Gender variable with 0/1 value, and associated labels are male/female, this function would convert all 0 to male and all 1 to female and returns the new variable as factor.

Usage

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

Arguments

x

A labelled vector (see set_labels or labelled), respectively a data frame with such variables.

...

Optional, unquoted names of variables. Required, if either x is a data frame and no vector, or if only selected variables from x should be used in the function. See 'Examples'.

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 set_label). 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).

Details

See 'Details' in get_na.

Value

A factor variable with the associated value labels as factor levels, or a data frame with such factor variables (if x was a data frame).

Note

Value label attributes (see, for instance, get_labels) will be removed when converting variables to factors.

See Also

to_factor to convert a numeric variable into a factor (and preserve labels); to_value to convert a factor into a numeric variable and to_character to convert a labelled vector into a character vector (using label attributes as values).

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
data(efc)
print(get_labels(efc)['c161sex'])
head(efc$c161sex)
head(to_label(efc$c161sex))

print(get_labels(efc)['e42dep'])
table(efc$e42dep)
table(to_label(efc$e42dep))

head(efc$e42dep)
head(to_label(efc$e42dep))

# structure of numeric values won't be changed
# by this function, it only applies to labelled vectors
# (typically categorical or factor variables)
str(efc$e17age)
str(to_label(efc$e17age))


# factor with non-numeric levels
to_label(factor(c("a", "b", "c")))

# factor with non-numeric levels, prefixed
x <- factor(c("a", "b", "c"))
set_labels(x) <- c("ape", "bear", "cat")
to_label(x, prefix = TRUE)


# create vector
x <- c(1, 2, 3, 2, 4, NA)
# add less labels than values
x <- set_labels(x, c("yes", "maybe", "no"),
                force.labels = FALSE,
                force.values = FALSE)
# convert to label w/o non-labelled values
to_label(x)
# convert to label, including non-labelled values
to_label(x, add.non.labelled = TRUE)


# create labelled integer, with missing flag
library(haven)
x <- labelled(c(1:3, tagged_na("a", "c", "z"), 4:1, 2:3),
              c("Agreement" = 1, "Disagreement" = 4, "First" = tagged_na("c"),
                "Refused" = tagged_na("a"), "Not home" = tagged_na("z")))
# to labelled factor, with missing labels
to_label(x, drop.na = FALSE)
# to labelled factor, missings removed
to_label(x, drop.na = TRUE)
# keep missings, and use non-labelled values as well
to_label(x, add.non.labelled = TRUE, drop.na = FALSE)


# convert labelled character to factor
dummy <- c("M", "F", "F", "X")
set_labels(dummy) <- c(`M` = "Male", `F` = "Female", `X` = "Refused")
get_labels(dummy,, "p")
to_label(dummy)

# drop unused factor levels, but preserve variable label
x <- factor(c("a", "b", "c"), levels = c("a", "b", "c", "d"))
set_labels(x) <- c("ape", "bear", "cat")
set_label(x) <- "A factor!"
x
to_label(x, drop.levels = TRUE)

# change variable label
to_label(x, var.label = "New variable label!", drop.levels = TRUE)


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

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.