-
Notifications
You must be signed in to change notification settings - Fork 36
Replace numeric coding with choice labels in your data
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 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.
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.RDS") # or wherever you stored the survey data you downloaded
survey_data %>%
mutate(degree_studied = choice_labels_for_values(survey_data, "degree_studied"))
Imagine your have three consecutive columns, degree_studied, current_job, annual_income and want to replace the numeric coding with the choice labels for all three.
Do this like so:
survey_data %>%
mutate(across(c(degree_studied, current_job, annual_income), ~choice_labels_for_values(survey_data, cur_column())))
Or, when the columns you want to replace are next to each other:
survey_data %>%
mutate(across(c(degree_studied:annual_income), ~choice_labels_for_values(survey_data, cur_column())))