Add labels for values that don't already have them.

lbl_add(x, ...)

lbl_add_vals(x, labeller = as.character, vals = NULL)

Arguments

x

A labelled vector

...

Labels formed by lbl indicating the value and label to be added.

labeller

A function that takes a single argument of the values and returns the labels. Defaults to as.character. as_function, so also accepts quosure-style lambda functions. See examples for more details.

vals

Vector of values to be labelled. NULL, the default labels all values that are in the data, but aren't already labelled.

Value

A haven::labelled vector

See also

Examples

x <- haven::labelled(
  c(100, 200, 105, 990, 999, 230),
  c(`Unknown` = 990, NIU = 999)
)

lbl_add(x, lbl(100, "$100"), lbl(105, "$105"), lbl(200, "$200"), lbl(230, "$230"))
#> <labelled<double>[6]>
#> [1] 100 200 105 990 999 230
#> 
#> Labels:
#>  value   label
#>    100    $100
#>    105    $105
#>    200    $200
#>    230    $230
#>    990 Unknown
#>    999     NIU

lbl_add_vals(x)
#> <labelled<double>[6]>
#> [1] 100 200 105 990 999 230
#> 
#> Labels:
#>  value   label
#>    100     100
#>    105     105
#>    200     200
#>    230     230
#>    990 Unknown
#>    999     NIU
lbl_add_vals(x, ~paste0("$", .))
#> <labelled<double>[6]>
#> [1] 100 200 105 990 999 230
#> 
#> Labels:
#>  value   label
#>    100    $100
#>    105    $105
#>    200    $200
#>    230    $230
#>    990 Unknown
#>    999     NIU
lbl_add_vals(x, vals = c(100, 200))
#> <labelled<double>[6]>
#> [1] 100 200 105 990 999 230
#> 
#> Labels:
#>  value   label
#>    100     100
#>    200     200
#>    990 Unknown
#>    999     NIU