Learn R Programming

lessR (version 2.5)

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 are present in the mylabels data frame.

Usage

label(x, dframe=mylabels)

Arguments

x
The variable for which to obtain the corresponding variable label.
dframe
Data frame that contains the variable of interest, which is mylabels.

Details

Standard R does not provide for variable labels, but lessR provides for a data frame called mylabels which stores variable labels. Read the labels into this data frame with the Read function and setting labels=TRUE. This setting can be accomplished with the short name for the Read function, rad. The variable labels can either be read from a separate file by themselves, rad.labels, or from the second row of the data file, rad.both.

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
# create data frame mydata (usually read from a file with rad)
n <- 12
X <- sample(c("Group1","Group2"), size=n, replace=TRUE)
Y <- rnorm(n=n, mean=50, sd=10)
mydata <- data.frame(X,Y)
rm(X); rm(Y);

# create data frame mylabels (usually read from a file with rad)
# The labels themselves are a variable named \code{label}, but 
#   this information is only necessary if constructing the 
#   labels data frame manually, such as is done here.
vname <- character(length=2)
vname[1] <- "X"
vname[2] <- "Y"
label <- character(length=2)
label[1] <- "Group"
label[2] <- "Response"
mylabels <- data.frame(label)
row.names(mylabels) <- vname

# 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(X), main=label(X)))
with(mydata, hist(Y, main=label(Y)))

Run the code above in your browser using DataLab