Makes sure that all variable labels are strings in a sense that
they are of character class and have length 1. If a label exists and it is
a string, it is remained unchanged, otherwise it is replaced with
NA_character_. When use_names = TRUE, missing or invalid labels,
including NA_character_, are replaced with the corresponding variable
names instead. Sometimes "label" attribute of a variable may contain
non-standard things like listing of all categories. It may make sense to
apply this function before doing something that uses variable labels, e.g.
crunch uploads.
Examples
library(dplyr)
data <-
mtcars |>
as_tibble() |>
select(mpg, cyl) |>
mutate(
car = rownames(mtcars),
cyl = factor(cyl)
)
attr(data$mpg, "label") <- "Miles per Gallon"
attr(data$cyl, "label") <- levels(data$cyl)
# label of cyl is a character vector containing more than one value
# If you e.g. call view(data), only the first value will be displayed
# this replaces the label of cyl with NA_character_,
# but does nothing to mpg's label
data2 <- srv_labels_fix(data)
#> ! Found 2 columns with missing or invalid labels: "cyl" and "car".
#> ℹ Replacing these labels with {.code NA_character_}
# use variable names instead of NA_character_
data3 <- srv_labels_fix(data, use_names = TRUE)
#> ! Found 2 columns with missing or invalid labels: "cyl" and "car".
#> ℹ Replacing these labels with variable names