Class Declaration

Class declaration

ilabelled offers the option of declaring individual vectors or all vectors in a data.frame as an object i_labelled. Additional meta information is added as attributes. Additional information can be viewed transparently using a class-specific print method.

In principle, adding additional information is optional. As mentioned above, the class-specific syntax is intended to encourage meta information to be included.

set.seed(1234)

myVar <- i_labelled(
  x = sample(c(1:3,-9), 50, replace = TRUE), 
  label = "Gender",
  labels = c(
    "A" = 1,
    "B" = 2,
    "C" = 3,
    "X" = -9
  ),
  na_values = -9,
  subject = "Personal information",
  wording = "What is your gender",
  scale = "nominal"
)
myVar
#> <i_labelled double>
#>  [1] -9 -9  2  2  1 -9  3  1  1  2 -9 -9  2  3  2  2  2  3  2 -9  2  2 -9  2 -9
#> [26] -9  1 -9 -9 -9  3 -9  3  3  1  2  1  2  2  3 -9  3 -9 -9 -9  3  3  1  3  2
#> 
#> Wording:
#>  What is your gender
#> 
#> Subject:
#>  Personal information 
#> 
#> Missing values: [-9]
#> 
#> Scale level: nominal
#> 
#> Variable label: 
#>  Gender
#> 
#> Value labels:
#>  value label
#>     -9     X
#>      1     A
#>      2     B
#>      3     C

factor to i_labelled

Value labels are automatically assigned for vectors of class factor. In contrast to the base R factor class, underlying values and labels can be addressed directly for i_labelled objects.

myData <- factor(c(1, 2, 3, NA), levels = 1:3, labels = c("A", "B", "C"))
myData <- i_labelled(myData)
myData
#> <i_labelled double>
#> [1]  1  2  3 NA
#> 
#> Value labels:
#>  value label
#>      1     A
#>      2     B
#>      3     C
myData %in% "A"
#> [1]  TRUE FALSE FALSE FALSE
myData == 1
#> [1]  TRUE FALSE FALSE    NA