as_label() converts (replaces) values of a variable (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. as_character() does the same as as_label(), but returns a character vector.

as_character(x, ...)

to_character(x, ...)

# S3 method for data.frame
as_character(
  x,
  ...,
  add.non.labelled = FALSE,
  prefix = FALSE,
  var.label = NULL,
  drop.na = TRUE,
  drop.levels = FALSE,
  keep.labels = FALSE
)

as_label(x, ...)

to_label(x, ...)

# S3 method for data.frame
as_label(
  x,
  ...,
  add.non.labelled = FALSE,
  prefix = FALSE,
  var.label = NULL,
  drop.na = TRUE,
  drop.levels = FALSE,
  keep.labels = FALSE
)

Arguments

x

A vector or data frame.

...

Optional, unquoted names of variables that should be selected for further processing. 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 tidyselect's select-helpers. 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 vignette Labelled Data and the sjlabelled-Package). 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).

keep.labels

Logical, if TRUE, value labels are preserved This allows users to quickly convert back factors to numeric vectors with as_numeric().

Value

A factor with the associated value labels as factor levels. If x

is a data frame, the complete data frame x will be returned, where variables specified in ... are coerced to factors; if ... is not specified, applies to all variables in the data frame. as_character() returns a character vector.

Details

See 'Details' in get_na.

Note

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

Examples

data(efc)
print(get_labels(efc)['c161sex'])
#> $c161sex
#> [1] "Male"   "Female"
#> 
head(efc$c161sex)
#> [1] 2 2 1 1 2 1
head(as_label(efc$c161sex))
#> [1] "<dbl>"

print(get_labels(efc)['e42dep'])
#> $e42dep
#> [1] "independent"          "slightly dependent"   "moderately dependent"
#> [4] "severely dependent"  
#> 
table(efc$e42dep)
#> 
#>   1   2   3   4 
#>  66 225 306 304 
table(as_label(efc$e42dep))
#> 
#> <dbl> 
#>     1 

head(efc$e42dep)
#> [1] 3 3 3 4 4 4
head(as_label(efc$e42dep))
#> [1] "<dbl>"

# 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)
#>  num [1:908] 83 88 82 67 84 85 74 87 79 83 ...
#>  - attr(*, "label")= chr "elder' age"
str(as_label(efc$e17age))
#>  chr "<dbl>"


# factor with non-numeric levels
as_label(factor(c("a", "b", "c")))
#> [1] "<fct>"

# factor with non-numeric levels, prefixed
x <- factor(c("a", "b", "c"))
x <- set_labels(x, labels = c("ape", "bear", "cat"))
as_label(x, prefix = TRUE)
#> Error in as_label(x, prefix = TRUE): unused argument (prefix = TRUE)


# create vector
x <- c(1, 2, 3, 2, 4, NA)
# add less labels than values
x <- set_labels(
  x,
  labels = c("yes", "maybe", "no"),
  force.labels = FALSE,
  force.values = FALSE
)
#> "x" has more values than "labels", hence not all values are labelled.

# convert to label w/o non-labelled values
as_label(x)
#> [1] "<dbl>"

# convert to label, including non-labelled values
as_label(x, add.non.labelled = TRUE)
#> Error in as_label(x, add.non.labelled = TRUE): unused argument (add.non.labelled = TRUE)


# create labelled integer, with missing flag
if (require("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
  as_label(x, drop.na = FALSE)

  # to labelled factor, missings removed
  as_label(x, drop.na = TRUE)

  # keep missings, and use non-labelled values as well
  as_label(x, add.non.labelled = TRUE, drop.na = FALSE)
}
#> Error in as_label(x, drop.na = FALSE): unused argument (drop.na = FALSE)

# convert labelled character to factor
dummy <- c("M", "F", "F", "X")
dummy <- set_labels(
  dummy,
  labels = c(`M` = "Male", `F` = "Female", `X` = "Refused")
)
get_labels(dummy,, "p")
#> [1] "[Female] F"  "[Male] M"    "[Refused] X"
as_label(dummy)
#> [1] "<chr>"

# drop unused factor levels, but preserve variable label
x <- factor(c("a", "b", "c"), levels = c("a", "b", "c", "d"))
x <- set_labels(x, labels = c("ape", "bear", "cat"))
set_label(x) <- "A factor!"
x
#> [1] a b c
#> attr(,"labels")
#>  ape bear  cat 
#>    a    b    c 
#> attr(,"label")
#> [1] A factor!
#> Levels: a b c d
as_label(x, drop.levels = TRUE)
#> Error in as_label(x, drop.levels = TRUE): unused argument (drop.levels = TRUE)

# change variable label
as_label(x, var.label = "New variable label!", drop.levels = TRUE)
#> Error in as_label(x, var.label = "New variable label!", drop.levels = TRUE): unused arguments (var.label = "New variable label!", drop.levels = TRUE)


# convert to numeric and back again, preserving label attributes
# *and* values in numeric vector
x <- c(0, 1, 0, 4)
x <- set_labels(x, labels = c(`null` = 0, `one` = 1, `four` = 4))

# to factor
as_label(x)
#> [1] "<dbl>"

# to factor, back to numeric - values are 1, 2 and 3,
# instead of original 0, 1 and 4
as_numeric(as_label(x))
#> [1] 1
#> attr(,"labels")
#> <dbl> 
#>     1 

# preserve label-attributes when converting to factor, use these attributes
# to restore original numeric values when converting back to numeric
as_numeric(as_label(x, keep.labels = TRUE), use.labels = TRUE)
#> Error in as_label(x, keep.labels = TRUE): unused argument (keep.labels = TRUE)


# easily coerce specific variables in a data frame to factor
# and keep other variables, with their class preserved
as_label(efc, e42dep, e16sex, c172code)
#> Error in as_label(efc, e42dep, e16sex, c172code): unused arguments (e42dep, e16sex, c172code)