labelled (version 2.7.0)

val_labels: Get / Set value labels

Description

Get / Set value labels

Usage

val_labels(x, prefixed = FALSE)

# S3 method for default val_labels(x, prefixed = FALSE)

# S3 method for haven_labelled val_labels(x, prefixed = FALSE)

# S3 method for data.frame val_labels(x, prefixed = FALSE)

val_labels(x) <- value

# S3 method for numeric val_labels(x) <- value

# S3 method for character val_labels(x) <- value

# S3 method for haven_labelled val_labels(x) <- value

# S3 method for haven_labelled_spss val_labels(x) <- value

# S3 method for data.frame val_labels(x) <- value

val_label(x, v, prefixed = FALSE)

# S3 method for haven_labelled val_label(x, v, prefixed = FALSE)

# S3 method for data.frame val_label(x, v, prefixed = FALSE)

val_label(x, v) <- value

# S3 method for haven_labelled val_label(x, v) <- value

# S3 method for numeric val_label(x, v) <- value

# S3 method for character val_label(x, v) <- value

# S3 method for data.frame val_label(x, v) <- value

set_value_labels(.data, ..., .labels = NA, .strict = TRUE)

add_value_labels(.data, ..., .strict = TRUE)

remove_value_labels(.data, ..., .strict = TRUE)

Arguments

x

A vector.

prefixed

Should labels be prefixed with values?

value

A named vector for val_labels() (see haven::labelled()) or a character string for val_labels(). NULL to remove the labels. For data frames, it could also be a named list with a vector of value labels per variable.

v

A single value.

.data

a data frame

...

name-value pairs of value labels (see examples)

.labels

value labels to be applied to the data.frame, using the same syntax as value in val_labels(df) <- value.

.strict

should an error be returned if some labels doesn't correspond to a column of x?

Value

val_labels() will return a named vector. val_label() will return a single character string.

set_value_labels(), add_value_labels() and remove_value_labels() will return an updated copy of .data.

Examples

Run this code
# NOT RUN {
v <- labelled(c(1,2,2,2,3,9,1,3,2,NA), c(yes = 1, no = 3, "don't know" = 9))
val_labels(v)
val_labels(v, prefixed = TRUE)
val_label(v, 2)
val_label(v, 2) <- 'maybe'
val_label(v, 9) <- NULL
val_labels(v) <- NULL
if (require(dplyr)) {
  # setting value labels
  df <- tibble(s1 = c("M", "M", "F"), s2 = c(1, 1, 2)) %>%
    set_value_labels(s1 = c(Male = "M", Female = "F"), s2 = c(Yes = 1, No = 2))
  val_labels(df)

  # updating value labels
  df <- df %>% add_value_labels(s2 = c(Unknown = 9))
  df$s2

  # removing a value labels
  df <- df %>% remove_value_labels(s2 = 9)
  df$s2

  # removing all value labels
  df <- df %>% set_value_labels(s2 = NULL)
  df$s2
}
# }

Run the code above in your browser using DataCamp Workspace