Label.Rd
Set and retrieve the label
, resp. unit
attribute of x
. This can be helpful for documenting the specific meaning of a variable, of an entire data.frame or any other object. For single vectors it can be useful to store the unit.
Label(x)
Label(x) <- value
Labels(x)
Labels(x) <- value
Unit(x)
Unit(x) <- value
The label should consist of a single text (length of 1). The text may contain line feeds.
It can be deleted by setting the label to NULL
.
Labels()
can be used to retrieve and assign vectorized labels to data.frames or lists.
Label
and Unit
return the label attribute of x, if any; otherwise, NULL.
A more elaborated label version can be found in package Hmisc label()
.
# add a descriptive label to a variable
Label(d.diamonds$colour) <- "The rating scale applied to diamonds ranges from colorless
to yellow, as any other color is extremely rare."
# technically just appending the text as attribute to the variable
attributes(d.diamonds$colour)
#> $levels
#> [1] "D" "E" "F" "G" "H" "I" "J" "K" "L"
#>
#> $class
#> [1] "factor"
#>
#> $label
#> [1] "The rating scale applied to diamonds ranges from colorless\nto yellow, as any other color is extremely rare."
#>
# label is supported while describing data
Desc(d.diamonds$colour)
#> ──────────────────────────────────────────────────────────────────────────────
#> d.diamonds$colour (factor) :
#> The rating scale applied to diamonds ranges from colorless to yellow,
#> as any other color is extremely rare.
#>
#>
#> length n NAs unique levels dupes
#> 440 440 0 9 9 y
#> 100.0% 0.0%
#>
#> level freq perc cumfreq cumperc
#> 1 I 79 18.0% 79 18.0%
#> 2 J 72 16.4% 151 34.3%
#> 3 H 71 16.1% 222 50.5%
#> 4 F 58 13.2% 280 63.6%
#> 5 E 54 12.3% 334 75.9%
#> 6 G 43 9.8% 377 85.7%
#> 7 K 31 7.0% 408 92.7%
#> 8 D 20 4.5% 428 97.3%
#> 9 L 12 2.7% 440 100.0%
#>
# The label can be deleted by setting it to NULL
Label(d.diamonds$colour) <- NULL
# Labelling the columns of a data.frame is best done with a loop
# (all so far seen *apply aproaches lead to more complicated code...)
lbl <- RndWord(16, 7)
for(i in seq_along(lbl))
Label(d.pizza[, i]) <- lbl[i]
Str(d.pizza)
#> 'data.frame': 1209 obs. of 16 variables:
#> 1 $ index : int 1 2 3 4 5 6 7 8 9 10 ...
#> ..- attr(*, "label")= chr "YUBIWKE"
#> 2 $ date : Date, format: "2014-03-01" "2014-03-01" "2014-03-01" "2014-03-01" ...
#> 3 $ week : num 9 9 9 9 9 9 9 9 9 9 ...
#> ..- attr(*, "label")= chr "AVTXXWD"
#> 4 $ weekday : num 6 6 6 6 6 6 6 6 6 6 ...
#> ..- attr(*, "label")= chr "ATOJFSU"
#> 5 $ area : Factor w/ 3 levels "Brent","Camden",..: 2 3 3 1 1 2 2 1 3 1 ...
#> ..- attr(*, "label")= chr "TUYNORH"
#> 6 $ count : int 5 2 3 2 5 1 4 NA 3 6 ...
#> ..- attr(*, "label")= chr "XNVXXCC"
#> 7 $ rabate : logi TRUE FALSE FALSE FALSE TRUE FALSE ...
#> ..- attr(*, "label")= chr "BVNIJXV"
#> 8 $ price : num 65.7 27 41 26 57.6 ...
#> ..- attr(*, "label")= chr "FUKFATC"
#> 9 $ operator : Factor w/ 3 levels "Allanah","Maria",..: 3 3 1 1 3 1 3 1 1 3 ...
#> ..- attr(*, "label")= chr "BNECAAF"
#> 10 $ driver : Factor w/ 7 levels "Butcher","Carpenter",..: 7 1 1 7 3 7 7 7 7 3 ...
#> ..- attr(*, "label")= chr "NXXDPHS"
#> 11 $ delivery_min : num 20 19.6 17.8 37.3 21.8 48.7 49.3 25.6 26.4 24.3 ...
#> ..- attr(*, "label")= chr "EIJACJU"
#> 12 $ temperature : num 53 56.4 36.5 NA 50 27 33.9 54.8 48 54.4 ...
#> ..- attr(*, "label")= chr "NGKZJKG"
#> 13 $ wine_ordered : int 0 0 0 0 0 0 1 NA 0 1 ...
#> ..- attr(*, "label")= chr "OYEYGIC"
#> 14 $ wine_delivered: int 0 0 0 0 0 0 1 NA 0 1 ...
#> ..- attr(*, "label")= chr "NTRZBOT"
#> 15 $ wrongpizza : logi FALSE FALSE FALSE FALSE FALSE FALSE ...
#> ..- attr(*, "label")= chr "CTHHRDE"
#> 16 $ quality : Ord.factor w/ 3 levels "low"<"medium"<..: 2 3 NA NA 2 1 1 3 3 2 ...
#> ..- attr(*, "label")= chr "JNOYIEN"