Skip to content

Replace numeric coding with choice labels in your data

Ulrik Lyngs edited this page Aug 20, 2021 · 4 revisions

You may have data from a survey where the responses now only have a numeric coding.

For example, you may have questions like "What degree do you study for?", with response options like "bachelor's degree", "master's degree", "doctoral degree", and "other" where the answers are now shown as 1, 2, 3, and 4. This is useful for some purposes, but not for others.

If you downloaded your data using the formr R package's formr_results function it is easy to replace your numeric coding with the choice labels. That's because the labels are included in the data object you downloaded. You can switch to the choice labels using the formr::choice_label_for_values function.

Replacing numeric values with choice labels for a single column

Imagine you have a column named degree_studied with the responses to the question "What degree do you study for?". Replace the numeric values in this column with the choice labels like so:

library(tidyverse)
library(formr)

survey_data <- readRDS("survey_data_full.RDS") # or wherever you stored the survey data you downloaded

survey_data %>%
  mutate(degree_studied = choice_labels_for_values(survey_data, "degree_studied"))

Replacing numeric values with choice labels for multiple columns

To replace numeric values with choice labels for all labelled columns, install and load the haven package (which includes the function is.labelled to tell us whether a column is labelled).

library(haven)
survey_data %>%
  mutate(across(where(is.labelled), ~choice_labels_for_values(survey_data, cur_column())))