Learn R Programming

lessR (version 3.5.5)

VariableLabels: Return or Assign a Variable Label

Description

Assign and/or display variable labels, displayed in the output of analyses either as text output at the console or graphics, such as an axis label on a graph. The variable labels can be assigned individually, or for some or all variables. To assign a single variable label, invoke the value option and assign the output to a specified data frame.

Usage

VariableLabels(x, value=NULL, data=mydata, quiet=getOption("quiet"))

Arguments

x
The file reference or character string variable (see examples) from which to obtain the variable labels, or a variable name for which to assign or obtain the corresponding variable label.
value
The variable label assigned to a specific variable, otherwise NULL.
data
Data frame that contains the variable(s) of interest. The output of the function is assigned to this data frame.
quiet
If set to TRUE, no text output. Can change system default with theme function.

Value

The data frame with the variable labels is returned.

Details

Standard R does not provide for variable labels, but lessR does. Read the labels by specifying a file reference as the first argument of the function call, or read the labels with the lessR Read function, as explained in the corresponding documentation. Individual variable labels can also be assigned with this function. Not all variables need have a label, and the variables with their corresponding labels can be listed or assigned in any order. When all or some of the labels are read, either from the console or an external csv or Excel file, each line of the file contains the variable name and then the associated variable label. The file types of .csv and .xlsx in the file reference listed in the first position of the function call are what trigger the interpretation of the argument as a file reference.

See Also

Read.

Examples

Run this code
# read the internal lessR data frame that contains variable labels
mydata <- Read("Employee", format="lessR")
# processing with a standard R transformation removes the labels
mydata <- transform(mydata, Years = Years)
# show that the labels are now gone
details.brief()


# construct and read variable labels from console
lbl <- "
Years,time of company employment
Gender,Male or Female
Dept,department employed
Salary,annual salary
Satisfaction,satisfaction with work environment
HealthPlan,1=GoodHealth 2=YellowCross 3=BestCare
"
mydata <- VariableLabels(lbl)

# read variable labels from a csv file
mydata <- Read("Employee", format="lessR")
mydata <- transform(mydata, Years = Years)  # remove the labels 
# mydata <- VariableLabels("http://lessRstats.com/data/employee_lbl.csv")

# add/modify a single variable label
mydata <- VariableLabels(Salary, "bucks buck")
details.brief()

# list the contents of a single variable label
VariableLabels(Salary)

# display all variable labels
VariableLabels()

Run the code above in your browser using DataLab