separate_longer_delim(),
separate_wider_delim()1. Restructure a data dictionary into long format
Review the dictionary
# A tibble: 3 × 4
var_name var_label var_type var_values
<chr> <chr> <chr> <chr>
1 tch_id Teacher study ID numeric 100-200
2 t_age What is your age? numeric 18-100
3 t_stress1 How often have you felt nervous or stressed? numeric 0 = Never | 1…
Here I want to convert my wide formatted data dictionary into long format. I want all of my variables to repeat a row for each response option and I want each row to only show one response option.
I also want the response option to be split out into a value column and a label column.
So here I will use separate_longer_delim() to first put
each response option on their own row. Then I use
separate_wider_delim() to split the response option value
and label into their own columns. If there are variables that only have
one option (like the ID and age variables), I added the argument
too_few to keep the values in the value column
dictionary %>%
separate_longer_delim(var_values, delim = "| ") %>%
separate_wider_delim(var_values, delim = " = ", names = c("value", "value_label"), too_few = "align_start")
# A tibble: 7 × 5
var_name var_label var_type value value_label
<chr> <chr> <chr> <chr> <chr>
1 tch_id Teacher study ID numeric 100-… <NA>
2 t_age What is your age? numeric 18-1… <NA>
3 t_stress1 How often have you felt nervous or stres… numeric 0 "Never "
4 t_stress1 How often have you felt nervous or stres… numeric 1 "Almost Ne…
5 t_stress1 How often have you felt nervous or stres… numeric 2 "Sometimes…
6 t_stress1 How often have you felt nervous or stres… numeric 3 "Fairly Of…
7 t_stress1 How often have you felt nervous or stres… numeric 4 "Very Ofte…
Return to Separate