Objects of type i_labelled can be very easily converted into base R
object classes. On the one hand, this is possible using the familiar
as.*
methods. On the other hand, there are specific
functions in which value labels are taken into account:
i_as_factor()
and i_as_character
If R’s own method as.* is used, the underlying data is converted accordingly. Value labels are not taken into account.
myData <- i_labelled(
x = c(1, 2, 3, NA),
labels = c("A" = 1, "B" = 2),
label = "my Variable"
)
as.character(myData)
#> [1] "1" "2" "3" NA
as.numeric(myData)
#> [1] 1 2 3 NA
as.factor(myData)
#> [1] 1 2 3 <NA>
#> Levels: 1 2 3
However, ilabelled also offers two specific functions that take value labels into account during conversion.
myData <- i_labelled(
x = c(1, 2, 3, NA, -9),
labels = c("A" = 1, "B" = 2, "X" = -9),
label = "my Variable",
na_values = -9
)
i_as_factor(myData, missing_to_na = FALSE, keep_attributes = FALSE)
#> [1] A B 3 <NA> X
#> Levels: X A B 3
i_as_character(myData, missing_to_na = TRUE, keep_attributes = TRUE)
#> [1] "A" "B" "3" NA NA
#> attr(,"label")
#> [1] "my Variable"
#> attr(,"labels")
#> A B
#> 1 2
#> attr(,"na_values")
#> [1] -9