Learn R Programming

lessR (version 2.8)

label: Apply a Variable Label to a non-lessR Function

Description

Display a variable label for output, either text output at the console or graphics, such as a title on a graph. Generally applies to standard R functions as lessR functions automatically provide variable labels on the output when the labels have been read with the data.

Usage

label(x, data=mydata)

Arguments

x
The variable for which to obtain the corresponding variable label.
data
Data frame that contains the variable of interest.

Details

Standard R does not provide for variable labels, but lessRdoes. Read the labels Read function and setting labels=TRUE. This setting can be accomplished with the short name for the Read function, rd.

Each row of the file that contains the labels, including the first row, consists of the variable name, a comma, and then the label, that is, standard csv format such as obtained with the csv option from a standard worksheet application such as Microsoft Excel or LibreOffice Calc. Not all variables in the data frame that contains the data, mydata by default, need have a label, and the variables with their corresponding labels can be listed in any order. Here is an example of this file for the dat.employee data file included in this lessR package. Years,"Years Employed in the Company" Gender,"Male or Female" Dept,"Department Employed" Salary,"Annual Salary (USD)" Satisfaction,"Satisfaction with Work Environment" HealthPlan,"1=GoodHealth, 2=YellowCross, 3=BestCare" Not all variables need have a label, and the variables with their corresponding labels can be listed in any order.

The label function is automatically accessed by the lessR functions that provide data analysis, such as automatically providing the title of a graph as the corresponding variable label. This function can also be added to standard R function calls as well, such as an argument for main in graphics output, where main is the title of the graph.

See Also

Read.

Examples

Run this code
# read the data with variable labels
mydata <- Read("Employee", format="lessR")

# variable label as the title of a graph from a standard R function
# the data are not attached, so for standard R functions, must
#   identify the relevant data frame, such as with function: with
with(mydata, barplot(table(Dept), main=label(Dept)))
with(mydata, hist(Salary, main=label(Salary)))

Run the code above in your browser using DataLab