This function retrieves the values associated with value labels
from labelled vectors. Data is also labelled
when imported from SPSS, SAS or STATA via read_spss,
read_sas or read_stata.
Arguments
- x
Variable (vector) with value label attributes; or a data frame or list with such variables.
- sort.val
Logical, if
TRUE(default), values of associated value labels are sorted.- drop.na
Logical, if
TRUE, tagged NA values are excluded from the return value. See 'Examples' andget_na.
Details
labelled vectors are numeric by default (when imported with read-functions
like read_spss) and have variable and value labels attributes.
The value labels are associated with the values from the labelled vector.
This function returns the values associated with the vector's value labels,
which may differ from actual values in the vector (e.g. if not all
values have a related label).
See also
get_labels for getting value labels and get_na
to get values for missing values.
Examples
data(efc)
str(efc$e42dep)
#> num [1:908] 3 3 3 4 4 4 4 4 4 4 ...
#> - attr(*, "label")= chr "elder's dependency"
#> - attr(*, "labels")= Named num [1:4] 1 2 3 4
#> ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
get_values(efc$e42dep)
#> [1] 1 2 3 4
get_labels(efc$e42dep)
#> [1] "independent" "slightly dependent" "moderately dependent"
#> [4] "severely dependent"
library(haven)
#> Error in library(haven): there is no package called ‘haven’
x <- labelled(c(1:3, tagged_na("a", "c", "z"), 4:1),
c("Agreement" = 1, "Disagreement" = 4, "First" = tagged_na("c"),
"Refused" = tagged_na("a"), "Not home" = tagged_na("z")))
#> Error in labelled(c(1:3, tagged_na("a", "c", "z"), 4:1), c(Agreement = 1, Disagreement = 4, First = tagged_na("c"), Refused = tagged_na("a"), `Not home` = tagged_na("z"))): could not find function "labelled"
# get all values
get_values(x)
#> Error: object 'x' not found
# drop NA
get_values(x, drop.na = TRUE)
#> Error: object 'x' not found
# data frame as input
y <- labelled(c(2:3, 3:1, tagged_na("y"), 4:1),
c("Agreement" = 1, "Disagreement" = 4, "Why" = tagged_na("y")))
#> Error in labelled(c(2:3, 3:1, tagged_na("y"), 4:1), c(Agreement = 1, Disagreement = 4, Why = tagged_na("y"))): could not find function "labelled"
get_values(data.frame(x, y))
#> Error: object 'x' not found
