# NOT RUN {
# manually set value and variable labels
dummy <- sample(1:4, 40, replace = TRUE)
dummy <- set_labels(dummy, labels = c("very low", "low", "mid", "hi"))
dummy <- set_label(dummy, label = "Dummy-variable")
# or use:
# set_label(dummy) <- "Dummy-variable"
# auto-detection of value labels by default, auto-detection of
# variable labels if argument "title" set to NULL.
# }
# NOT RUN {
library(sjPlot)
sjp.frq(dummy, title = NULL)
# }
# NOT RUN {
# Set variable labels for data frame
dummy <- data.frame(
a = sample(1:4, 10, replace = TRUE),
b = sample(1:4, 10, replace = TRUE),
c = sample(1:4, 10, replace = TRUE)
)
dummy <- set_label(dummy, c("Variable A", "Variable B", "Variable C"))
str(dummy)
# remove one variable label
dummy <- set_label(dummy, c("Variable A", "", "Variable C"))
str(dummy)
# setting same variable labels to multiple vectors
# create a set of dummy variables
dummy1 <- sample(1:4, 40, replace = TRUE)
dummy2 <- sample(1:4, 40, replace = TRUE)
dummy3 <- sample(1:4, 40, replace = TRUE)
# put them in list-object
dummies <- list(dummy1, dummy2, dummy3)
# and set variable labels for all three dummies
dummies <- set_label(dummies, c("First Dummy", "2nd Dummy", "Third dummy"))
# see result...
get_label(dummies)
# use 'var_labels()' to set labels within a pipe-workflow, and
# when you need "tidyverse-consistent" api.
# Set variable labels for data frame
dummy <- data.frame(
a = sample(1:4, 10, replace = TRUE),
b = sample(1:4, 10, replace = TRUE),
c = sample(1:4, 10, replace = TRUE)
)
library(magrittr)
dummy %>%
var_labels(a = "First variable", c = "third variable") %>%
get_label()
# with quasi-quotation
library(rlang)
v1 <- "First variable"
v2 <- "Third variable"
dummy %>%
var_labels(a = !!v1, c = !!v2) %>%
get_label()
x1 <- "a"
x2 <- "c"
dummy %>%
var_labels(!!x1 := !!v1, !!x2 := !!v2) %>%
get_label()
# }
Run the code above in your browser using DataLab