| val_lab {expss} | R Documentation |
Set or get value labels
Description
These functions set/get/drop value labels. Duplicated values are not allowed.
If argument x is data.frame or list then labels applied to all
elements of data.frame/list. To drop value labels, use val_lab(var) <-
NULL or unvl(var). make_labels converts text from the form
that usually used in questionnaires to named vector. For variable labels see
var_lab. For working with entire data.frame see apply_labels.
val_labreturns value labels or NULL if labels doesn't exist.val_lab<-set value labels.set_val_labreturns variable with value labels.add_val_lab<-add value labels to already existing value labels.unvldrops value labels.make_labelsmakes named vector from text for usage as value labels.num_lab,lab_numandautonumare shortcuts formake_labelswithcode_postion'left', 'right' and 'autonum' accordingly.
Usage
val_lab(x)
val_lab(x) <- value
set_val_lab(x, value, add = FALSE)
add_val_lab(x, value)
add_val_lab(x) <- value
unvl(x)
drop_val_labs(x)
make_labels(text, code_position = c("left", "right", "autonum"))
drop_unused_labels(x)
num_lab(text)
lab_num(text)
autonum(text)
Arguments
x |
Variable(s). Vector/data.frame/list. |
value |
Named vector. Names of vector are labels for the appropriate values of variable x. |
add |
Logical. Should we add value labels to old labels or replace it? Deafult is FALSE - we completely replace old values. If TRUE new value labels will be combined with old value labels. |
text |
text that should be converted to named vector |
code_position |
Possible values "left", "right" - position of numeric code in
|
Details
Value labels are stored in attribute "labels"
(attr(x,"labels")). We set variable class to "labelled" for preserving
labels from dropping during some operations (such as c and `[`).
Value
val_lab return value labels (named vector). If labels doesn't
exist it return NULL . val_lab<- and set_val_lab return
variable (vector x) of class "labelled" with attribute "labels" which
contains value labels. make_labels return named vector for usage as
value labels.
Examples
# toy example
data.table::setDTthreads(2)
set.seed(123)
# score - evaluation of tested product
score = sample(-1:1,20,replace = TRUE)
var_lab(score) = "Evaluation of tested brand"
val_lab(score) = c("Dislike it" = -1,
"So-so" = 0,
"Like it" = 1
)
# frequency of product scores
fre(score)
# brands - multiple response question
# Which brands do you use during last three months?
brands = as.sheet(t(replicate(20,sample(c(1:5,NA),4,replace = FALSE))))
var_lab(brands) = "Used brands"
val_lab(brands) = make_labels("
1 Brand A
2 Brand B
3 Brand C
4 Brand D
5 Brand E
")
# percentage of used brands
fre(brands)
# percentage of brands within each score
cro_cpct(brands, score)
## make labels from text copied from questionnaire
age = c(1, 2, 1, 2)
val_lab(age) = num_lab("
1. 18 - 26
2. 27 - 35
")
# note support of value lables in base R
table(age)
# or, if in original codes is on the right side
products = 1:8
val_lab(products) = lab_num("
Chocolate bars 1
Chocolate sweets (bulk) 2
Slab chocolate(packed) 3
Slab chocolate (bulk) 4
Boxed chocolate sweets 5
Marshmallow/pastilles in chocolate coating 6
Marmalade in chocolate coating 7
Other 8
")
table(products)