Learn R Programming

Hmisc (version 5.3-1)

label: Label Attribute of an Object

Description

label(x) retrieves the label attribute of x. label(x) <- "a label" stores the label attribute, and also puts the class labelled as the first class of x (for S-Plus this class is not used and methods for handling this class are not defined so the "label" and "units" attributes are lost upon subsetting). The reason for having this class is so that the subscripting method for labelled, [.labelled, can preserve the label attribute in S. Also, the print method for labelled objects prefaces the print with the object's label (and units if there). If the variable is also given a "units" attribute using the units function, subsetting the variable (using [.labelled) will also retain the "units" attribute.

label can optionally append a "units" attribute to the string, and it can optionally return a string or expression (for R's plotmath facility) suitable for plotting. labelPlotmath is a function that also has this function, when the input arguments are the 'label' and 'units' rather than a vector having those attributes. When plotmath mode is used to construct labels, the 'label' or 'units' may contain math expressions but they are typed verbatim if they contain percent signs, blanks, or underscores. labelPlotmath can optionally create the expression as a character string, which is useful in building ggplot commands.

For Surv objects, label first looks to see if there is an overall "label" attribute for the object, then it looks for saved attributes that Surv put in the "inputAttributes" object, looking first at the event variable, then time2, and finally time. You can restrict the looking by specifying type.

labelLatex constructs suitable LaTeX labels a variable or from the label and units arguments, optionally right-justifying units if hfill=TRUE. This is useful when making tables when the variable in question is not a column heading. If x is specified, label and units values are extracted from its attributes instead of from the other arguments.

Label (actually Label.data.frame) is a function which generates S source code that makes the labels in all the variables in a data frame easy to edit.

llist is like list except that it preserves the names or labels of the component variables in the variables label attribute. This can be useful when looping over variables or using sapply or lapply. By using llist instead of list one can annotate the output with the current variable's name or label. llist also defines a names attribute for the list and pulls the names from the arguments' expressions for non-named arguments.

prList prints a list with element names (without the dollar sign as in default list printing) and if an element of the list is an unclassed list with a name, all of those elements are printed, with titles of the form "primary list name : inner list name". This is especially useful for Rmarkdown html notebooks when a user-written function creates multiple html and graphical outputs to all be printed in a code chunk. Optionally the names can be printed after the object, and the htmlfig option provides more capabilities when making html reports. prList does not work for regular html documents.

putHfig is similar to prList but for a single graphical object that is rendered with a print method, making it easy to specify long captions, and short captions for the table of contents in HTML documents. Table of contents entries are generated with the short caption, which is taken as the long caption if there is none. One can optionally not make a table of contents entry. If argument table=TRUE table captions will be produced instead. Using expcoll, markupSpecs html function expcoll will be used to make tables expand upon clicking an arrow rather than always appear.

putHcap is like putHfig except that it assumes that users render the graphics or table outside of the putHcap call. This allows things to work in ordinary html documents. putHcap does not handle collapsed text.

plotmathTranslate is a simple function that translates certain character strings to character strings that can be used as part of R plotmath expressions. If the input string has a space or percent inside, the string is surrounded by a call to plotmath's paste function.

as.data.frame.labelled is a utility function that is called by [.data.frame. It is just a copy of as.data.frame.vector. data.frame.labelled is another utility function, that adds a class "labelled" to every variable in a data frame that has a "label" attribute but not a "labelled" class.

relevel.labelled is a method for preserving labels with the relevel function.

reLabelled is used to add a 'labelled' class back to variables in data frame that have a 'label' attribute but no 'labelled' class. Useful for changing cleanup.import()'d S-Plus data frames back to general form for R and old versions of S-Plus.

Usage

label(x, default=NULL, ...)

# S3 method for default label(x, default=NULL, units=plot, plot=FALSE, grid=FALSE, html=FALSE, ...)

# S3 method for Surv label(x, default=NULL, units=plot, plot=FALSE, grid=FALSE, html=FALSE, type=c('any', 'time', 'event'), ...)

# S3 method for data.frame label(x, default=NULL, self=FALSE, ...)

label(x, ...) <- value

# S3 method for default label(x, ...) <- value

# S3 method for data.frame label(x, self=TRUE, ...) <- value

labelPlotmath(label, units=NULL, plotmath=TRUE, html=FALSE, grid=FALSE, chexpr=FALSE)

labelLatex(x=NULL, label='', units='', size='smaller[2]', hfill=FALSE, bold=FALSE, default='', double=FALSE)

# S3 method for labelled print(x, ...) ## or x - calls print.labelled

Label(object, ...)

# S3 method for data.frame Label(object, file='', append=FALSE, ...)

llist(..., labels=TRUE)

prList(x, lcap=NULL, htmlfig=0, after=FALSE)

putHfig(x, ..., scap=NULL, extra=NULL, subsub=TRUE, hr=TRUE, table=FALSE, file='', append=FALSE, expcoll=NULL)

putHcap(..., scap=NULL, extra=NULL, subsub=TRUE, hr=TRUE, table=FALSE, file='', append=FALSE)

plotmathTranslate(x)

data.frame.labelled(object)

# S3 method for labelled relevel(x, ...)

reLabelled(object)

combineLabels(...)

Arguments

Value

label returns the label attribute of x, if any; otherwise, "".

label is used most often for the individual variables in data frames. The function

sas.get copies labels over from SAS if they exist.

See Also

sas.get, describe, extractlabs, hlab

Examples

Run this code
age <- c(21,65,43)
y   <- 1:3
label(age) <- "Age in Years"
plot(age, y, xlab=label(age))

data <- data.frame(age=age, y=y)
label(data)

label(data, self=TRUE) <- "A data frame"
label(data, self=TRUE)

x1 <- 1:10
x2 <- 10:1
label(x2) <- 'Label for x2'
units(x2) <- 'mmHg'
x2
x2[1:5]
dframe <- data.frame(x1, x2)
Label(dframe)

labelLatex(x2, hfill=TRUE, bold=TRUE)
labelLatex(label='Velocity', units='m/s')

##In these examples of llist, note that labels are printed after
##variable names, because of print.labelled
a <- 1:3
b <- 4:6
label(b) <- 'B Label'
llist(a,b)
llist(a,b,d=0)
llist(a,b,0)


w <- llist(a, b>5, d=101:103)
sapply(w, function(x){
  hist(as.numeric(x), xlab=label(x))
  # locator(1)   ## wait for mouse click
})

# Or: for(u in w) {hist(u); title(label(u))}

Run the code above in your browser using DataLab