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)

Arguments

x

Variable to assign value labels to

value_labels

Named character vector to be assigned as value labels

See also

Other Labels: set_varl()

Examples


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?"
#>