Learn R Programming

expss (version 0.5.5)

val_lab: 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. See examples. For utilizing labels in base R see f, names2labels, values2labels, unlab, dichotomy. For variable labels see var_lab.
  • val_lab returns value labels or NULL if labels doesn't exist.
  • val_lab<- set value labels.
  • set_val_lab returns variable with value labels.
  • add_val_lab<- add value labels to already existing value labels.
  • unvl drops value labels.
  • make_labels makes named vector from text for usage as value labels.
  • ml_left, ml_right and ml_autonum are shortcuts for make_labels with code_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)
make_labels(text, code_position = c("left", "right", "autonum"))
ml_left(text)
ml_right(text)
ml_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 text. "autonum" - makes codes by autonumbering lines of text.

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.

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 `[`). There are special methods of subsetting and concatenation for this class.

Examples

Run this code
# toy example
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 = 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(brands, score)


aggregate(dichotomy(brands) ~ f(score), FUN = mean)

# customer segmentation by used brands
kmeans(dichotomy(brands),3)

# model of influence of used brands on evaluation of tested product 
summary(lm(score ~ dichotomy(brands)))

## make labels from text copied from questionnaire

age = c(1, 2, 1, 2)

val_lab(age) = make_labels("
 1. 18 - 26
 2. 27 - 35
")

f(age)

# or, if in original codes is on the right side

products = 1:8

val_lab(products) = ml_right("
 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
")

f(products)

Run the code above in your browser using DataLab