var_lab: Set or get variable label

Description Usage Arguments Details Value Examples

View source: R/labels.R

Description

These functions set/get/drop variable labels. For value labels see val_lab. For working with entire data.frame see apply_labels.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
var_lab(x, default = NULL)

var_lab(x) <- value

set_var_lab(x, value)

unvr(x)

drop_var_labs(x)

add_labelled_class(
  x,
  remove_classes = c("haven_labelled", "spss_labelled", "haven_labelled_spss",
    "vctrs_vctr")
)

Arguments

x

Variable. In the most cases it is numeric vector.

default

A character scalar. What we want to get from 'var_lab' if there is no variable label. NULL by default.

value

A character scalar - label for the variable x.

remove_classes

A character vector of classes which should be removed from the class attribute of the x.

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).

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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)"
                val_lab(am) = c(automatic = 0, manual=1)
                var_lab(gear) = "Number of forward gears"
                var_lab(carb) = "Number of carburetors"
})

fre(mtcars$am)

calculate(mtcars, 
     cro_mean(list(mpg, disp, hp, qsec), list(total(), am))
     ) 

 
## Not run: 
if(FALSE){ # to prevent execution
# you need to load packages strictly in this order to avoid conflicts
library(haven)
library(expss)
spss_data = haven::read_spss("spss_file.sav")
# add missing 'labelled' class
spss_data = add_labelled_class(spss_data) 
}

## End(Not run)

Example output

                                                                               
 | Transmission (0 = automatic, 1 = manual) | Count | Valid percent | Percent |
 | ---------------------------------------- | ----- | ------------- | ------- |
 |                                automatic |    19 |          59.4 |    59.4 |
 |                                   manual |    13 |          40.6 |    40.6 |
 |                                   #Total |    32 |         100.0 |   100.0 |
 |                                     <NA> |     0 |               |     0.0 |
                                         
 Responses, % | Cumulative responses, % |
 ------------ | ----------------------- |
         59.4 |                    59.4 |
         40.6 |                   100.0 |
        100.0 |                         |
              |                         |
                                                                              
 |                       | #Total | Transmission (0 = automatic, 1 = manual) |
 |                       |        |                                automatic |
 | --------------------- | ------ | ---------------------------------------- |
 |     Miles/(US) gallon |   20.1 |                                     17.1 |
 | Displacement (cu.in.) |  230.7 |                                    290.4 |
 |      Gross horsepower |  146.7 |                                    160.3 |
 |         1/4 mile time |   17.8 |                                     18.2 |
         
        |
 manual |
 ------ |
   24.4 |
  143.5 |
  126.8 |
   17.4 |

expss documentation built on Jan. 8, 2021, 5:38 p.m.