library(tinylabels)
<- 1:4
x variable_label(x) <- "A variable label"
If elements of a vector are extracted, the new vector retains label and class tiny_labelled
.
1:2]
x[#> Variable label : A variable label
#> [1] 1 2
str(x)
#> 'tiny_labelled' int [1:4] 1 2 3 4
#> - attr(*, "label")= chr "A variable label"
1:2] <- 5:6
x[str(x)
#> 'tiny_labelled' int [1:4] 5 6 3 4
#> - attr(*, "label")= chr "A variable label"
If a vector is modified (e.g., via mathematical operations), label and tiny_labelled
class are removed.
str(exp(x))
#> num [1:4] 148.4 403.4 20.1 54.6
str(x + 1)
#> num [1:4] 6 7 4 5
str(min(x))
#> int 3
str(Re(x))
#> num [1:4] 5 6 3 4
Vectors of class tiny_labelled
keep label and class if they are modified via as.character()
, as.numeric()
, etc.
as.character(x)
#> Variable label : A variable label
#> [1] "5" "6" "3" "4"
If keep_label = FALSE
, label and class are removed.
as.character(x, keep_label = FALSE)
#> [1] "5" "6" "3" "4"
as()
methods are always strict, i.e. label and class are removed:
as(x, "character")
#> [1] "5" "6" "3" "4"
It is possible to assign multiple labels to the columns of a data frame by passing a named list or a named vector of key-value pairs. Note that mixing different types of labels (e.g. character
and expression
labels) is only possible if the right-hand side is a list.
variable_label(npk) <- c(
N = "Nitrogen"
P = "Phosphate"
,
)variable_label(npk) <- list(
yield = expression(bar(Yield))
)variable_label(npk)
#> $block
#> NULL
#>
#> $N
#> [1] "Nitrogen"
#>
#> $P
#> [1] "Phosphate"
#>
#> $K
#> NULL
#>
#> $yield
#> expression(bar(Yield))