Learn R Programming

expss (version 0.5.5)

var_lab: Set or get variable label

Description

These functions set/get/drop variable labels. For utilizing labels in base R see f, names2labels, values2labels, unlab. For value labels see val_lab.
  • var_lab returns variable label or NULL if label doesn't exist.
  • var_lab<- set variable label.
  • set_var_lab returns variable with label.
  • unvr drops variable label.

Usage

var_lab(x)
var_lab(x) <- value
set_var_lab(x, value)
unvr(x)

Arguments

x
Variable. In the most cases it is numeric vector.
value
A character scalar - label for the variable x.

Value

var_lab return variable label. If label doesn't exist it return NULL . var_lab<- and set_var_lab return variable (vector x) of class "labelled" with attribute "label" which equals submitted value.

Details

Variable label is stored in attribute "label" (attr(x,"label")). For preserving from dropping this attribute during some operations (such as c) variable class is set to "labelled". There are special methods of subsetting and concatenation for this class. To drop variable label use var_lab(var) <- NULL or unvr(var).

Examples

Run this code
data(mtcars)
mtcars = modify(mtcars,{
                var_lab(mpg) = "Miles/(US) gallon"
                var_lab(cyl) = "Number of cylinders"
                var_lab(disp) = "Displacement (cu.in.)"
                var_lab(hp) = "Gross horsepower"
                var_lab(drat) = "Rear axle ratio"
                var_lab(wt) = "Weight (lb/1000)"
                var_lab(qsec) = "1/4 mile time"
                var_lab(vs) = "V/S"
                var_lab(am) = "Transmission (0 = automatic, 1 = manual)"
                var_lab(gear) = "Number of forward gears"
                var_lab(carb) = "Number of carburetors"
})

# note: we exclude dependent variable 'mpg' from conversion to use its short name in formula
summary(lm(mpg ~ ., data = n2l(mtcars, exclude = "mpg")))

data(mtcars)

var_lab(mtcars$am) = "Transmission"
val_lab(mtcars$am) = c(automatic = 0, manual=1)

## Not run: 
# plot(f(mtcars$am))
# ## End(Not run)

fre(mtcars$am)

Run the code above in your browser using DataLab