set_variable_labels()1. Set labels for one or more variables (Var1, Var2, and Var3)
Review the data (d4)
# A tibble: 3 x 3
Var1 Var2 Var3
<chr> <dbl> <dbl>
1 a 1 2
2 b NA 3
3 c 3 1
Add variable labels
d4 <- d4 %>%
labelled::set_variable_labels(
Var1 = "Name", Var2 = "Interest in Homework",
Var3 = "Interest in School"
)
labelled::var_label(d4)
$Var1
[1] "Name"
$Var2
[1] "Interest in Homework"
$Var3
[1] "Interest in School"
2. Derive variable labels from variable names (simple example)
Review the data (d7)
# A tibble: 3 x 3
student_id map_ela_20 map_math_20
<dbl> <dbl> <dbl>
1 1 100 400
2 2 200 200
3 3 400 100
Set variable labels by adding the base::names() function
which retrieves the names of objects
d7 <- d7 %>%
labelled::set_variable_labels(.labels = names(d7))
d7 %>%
labelled::var_label()
$student_id
[1] "student_id"
$map_ela_20
[1] "map_ela_20"
$map_math_20
[1] "map_math_20"
3. Derive variable labels from variable names (add sentence case)
Review the data (d7)
# A tibble: 3 x 3
student_id map_ela_20 map_math_20
<dbl> <dbl> <dbl>
1 1 100 400
2 2 200 200
3 3 400 100
Set variable labels by adding the
snakecase::to_sentence_case() function in addition to the
base::names() function.
d7 <- d7 %>%
labelled::set_variable_labels(.labels = snakecase::to_sentence_case(names(d7)))
d7 %>%
labelled::var_label()
$student_id
[1] "Student id"
$map_ela_20
[1] "Map ela 20"
$map_math_20
[1] "Map math 20"
4. Derive variable labels from variable names (add additional info to labels)
Review the data (d7)
# A tibble: 3 x 3
student_id map_ela_20 map_math_20
<dbl> <dbl> <dbl>
1 1 100 400
2 2 200 200
3 3 400 100
Set variable labels by adding the
stringr::str_replace_all() function in addition to the
base::names() function
d7 <- d7 %>%
labelled::set_variable_labels(
.labels = stringr::str_replace_all(names(.), c(
"_" = " ",
"map" = "map score",
"math" = "in mathematics",
"ela" = "in english language arts",
"20" = "19-20 school year"
))
)
d7 %>%
labelled::var_label()
$student_id
[1] "student id"
$map_ela_20
[1] "map score in english language arts 19-20 school year"
$map_math_20
[1] "map score in mathematics 19-20 school year"
5. Derive variable labels from variable names (add additional info to labels and use sentence case)
Review the data (d7)
# A tibble: 3 x 3
student_id map_ela_20 map_math_20
<dbl> <dbl> <dbl>
1 1 100 400
2 2 200 200
3 3 400 100
Set variable labels by adding the
stringr::str_replace_all() function in addition to the
snakecase::to_sentence_case() and the
base::names() function
d7 <- d7 %>%
labelled::set_variable_labels(
.labels = snakecase::to_sentence_case(stringr::str_replace_all(names(.), c(
"_" = " ",
"map" = "map score",
"math" = "in mathematics",
"ela" = "in english language arts",
"20" = "19-20 school year"
)))
)
d7 %>%
labelled::var_label()
$student_id
[1] "Student id"
$map_ela_20
[1] "Map score in english language arts 19 20 school year"
$map_math_20
[1] "Map score in mathematics 19 20 school year"
Return to Label Data