read_spss
,
read_sas
or read_stata
) and
x
is a data frame or list of variables, returns all variables' value labels as list
x
is a vector, returns the value labels as string.
get_labels(x, attr.only = FALSE, include.values = NULL, include.non.labelled = FALSE)
data.frame
with variables that have value label attributes (e.g.
from an imported SPSS, SAS or STATA data set, via read_spss
,
read_sas
or read_stata
); a variable
(vector) with value label attributes; or a list
of variables
with values label attributes. If x
has no label attributes,
factor levels are returned. See 'Examples'.TRUE
, labels are only searched for
in the the vector's attributes
; else, if x
has no
label attributes, factor levels or string values are returned. See
'Examples'.include.values = "as.name"
(or include.values = "n"
), values are set as names
attribute of the returned object. If include.values = "as.prefix"
(or include.values = "p"
), values are included as prefix
to each label. See 'Examples'.TRUE
, values without labels will
also be included in the returned labels.x
is a data.frame
or list
; a string with the value
labels, if x
is a variable;
or NULL
if no value label attribute was found.
get_label
or get_labels
to get a vector of value and variable labels, which can then be
used with other functions like barplot
etc.
See 'Examples'.
Furthermore, value and variable labels are used when saving data, e.g. to SPSS
(see write_spss
), which means that the written SPSS file
contains proper labels for each variable.
You can set a default label style (i.e. the names of the label
attributes, see above) via options(value_labels = "haven")
or options(value_labels = "foreign")
.
set_labels
to manually set value labels, get_label
to get variable labels and get_values
to retrieve value label associated values.
# import SPSS data set
# mydat <- read_spss("my_spss_data.sav")
# retrieve variable labels
# mydat.var <- get_label(mydat)
# retrieve value labels
# mydat.val <- get_labels(mydat)
data(efc)
get_labels(efc$e42dep)
# simple barplot
barplot(table(efc$e42dep))
# get value labels to annotate barplot
barplot(table(efc$e42dep),
names.arg = get_labels(efc$e42dep),
main = get_label(efc$e42dep))
# include associated values
get_labels(efc$e42dep, include.values = "as.name")
# include associated values
get_labels(efc$e42dep, include.values = "as.prefix")
# get labels from multiple variables
get_labels(list(efc$e42dep, efc$e16sex, efc$e15relat))
# create a dummy factor
f1 <- factor(c("hi", "low", "mid"))
# search for label attributes only
get_labels(f1, attr.only = TRUE)
# search for factor levels as well
get_labels(f1)
# same for character vectors
c1 <- c("higher", "lower", "mid")
# search for label attributes only
get_labels(c1, attr.only = TRUE)
# search for string values as well
get_labels(c1)
# create vector
x <- c(1, 2, 3, 2, 4, NA)
# add less labels than values
x <- set_labels(x, c("yes", "maybe", "no"), force.values = FALSE)
# get labels for labelled values only
get_labels(x)
# get labels for all values
get_labels(x, include.non.labelled = TRUE)
Run the code above in your browser using DataLab