chr_to_var | R Documentation |
This function takes a string variable and converts it into a labelled double
variable, using value label mappings (lab_str
, lab_num
) and variable
labels (var_label
) provided by the user.
chr_to_var(var, lab_str, lab_num, var_label, na = "<N/A>", wrap = 100)
var |
string vector containing the variable |
lab_str |
string vector containing the labels for the variable. The
lengths and values must be an exact match of the variable in |
lab_num |
numeric vector matching the labels provided in |
var_label |
string to be used as the variable label, passed through to
|
na |
string to replace missing values with as a value label. Defaults to
|
wrap |
numeric value to apply a text wrap to value labels for plotting
aesthetics. The numeric value determines the number of minimum characters
before the next space before adding a new line ( |
This function is a wrapper around several other surveytoolbox functions:
create_named_list()
set_vall()
set_varl()
a labelled double variable
var1 <- c("Yes", "No", "Not sure")
lab1 <- c(1, 0, 99)
q1_var <- sample(var1, 100, replace = TRUE)
# Convert to labelled double
chr_to_var(
var = q1_var,
lab_str = var1,
lab_num = lab1,
var_label = c("Have you come across this product previously?")
)
## Example with missing values
# Generate variable with missing values
var1 <- c("Yes", "No", NA)
q1_var <- sample(var1, 100, replace = TRUE)
# Variable and value labels
var1b <- c("Yes", "No", "<N/A>")
lab1 <- c(1, 0, 99)
# Convert to labelled double
chr_to_var(
var = q1_var,
lab_str = var1b,
lab_num = lab1,
var_label = c("Have you come across this product previously?")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.