A pipe-workflow optimised method to set value labels.
This is a wrapper around haven::labelled()
, but preserves
the original variable label of the vector.
set_vall(x, value_labels)
Other Labels:
set_varl()
library(magrittr)
library(dplyr)
library(tibble)
tibble(RESPID=1:1000,
Q1=sample(c(0,1,2),1000,replace=TRUE),
Q2=sample(c(0,1),1000,replace=TRUE))-> df
df %>%
mutate_at("Q2",funs(set_varl(.,"What is your answer to this yes/no question?"))) %>%
mutate_at("Q2",funs(set_vall(.,c("No"=0,"Yes"=1)))) %>%
.$Q2 %>% attributes()
#> Warning: `funs()` was deprecated in dplyr 0.8.0.
#> ℹ Please use a list of either functions or lambdas:
#>
#> # Simple named list: list(mean = mean, median = median)
#>
#> # Auto named with `tibble::lst()`: tibble::lst(mean, median)
#>
#> # Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
#> Warning: `funs()` was deprecated in dplyr 0.8.0.
#> ℹ Please use a list of either functions or lambdas:
#>
#> # Simple named list: list(mean = mean, median = median)
#>
#> # Auto named with `tibble::lst()`: tibble::lst(mean, median)
#>
#> # Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
#> $labels
#> No Yes
#> 0 1
#>
#> $class
#> [1] "haven_labelled" "vctrs_vctr" "double"
#>
#> $label
#> [1] "What is your answer to this yes/no question?"
#>