Format a Data Frame or Matrix for LaTeX or HTML
format.df.Rdformat.df does appropriate rounding and decimal alignment, and outputs
a character matrix containing the formatted data. If x is a
data.frame, then do each component separately.
If x is a matrix, but not a data.frame, make it a data.frame
with individual components for the columns.
If a component x$x is a matrix, then do all columns the same.
Usage
format.df(x, digits, dec=NULL, rdec=NULL, cdec=NULL,
numeric.dollar=!dcolumn, na.blank=FALSE, na.dot=FALSE,
blank.dot=FALSE, col.just=NULL, cdot=FALSE,
dcolumn=FALSE, matrix.sep=' ', scientific=c(-4,4),
math.row.names=FALSE, already.math.row.names=FALSE,
math.col.names=FALSE, already.math.col.names=FALSE,
double.slash=FALSE, format.Date="%m/%d/%Y",
format.POSIXt="%m/%d/%Y %H:%M:%OS", ...)Arguments
- x
a matrix (usually numeric) or data frame
- digits
causes all values in the table to be formatted to
digitssignificant digits.decis usually preferred.- dec
If
decis a scalar, all elements of the matrix will be rounded todecdecimal places to the right of the decimal.deccan also be a matrix whose elements correspond tox, for customized rounding of each element. A matrixdecmust have number of columns equal to number of columns of inputx. A scalardecis expanded to a vectorcdecwith number of items equal to number of columns of inputx.- rdec
a vector specifying the number of decimal places to the right for each row (
cdecis more commonly used thanrdec) A vectorrdecmust have number of items equal to number of rows of inputx.rdecis expanded to matrixdec.- cdec
a vector specifying the number of decimal places for each column. The vector must have number of items equal to number of columns or components of input x.
- cdot
Set to
TRUEto use centered dots rather than ordinary periods in numbers. The output uses a syntax appropriate forlatex.- na.blank
Set to
TRUEto use blanks rather thanNAfor missing values. This usually looks better inlatex.- dcolumn
Set to
TRUEto use David Carlisle's dcolumn style for decimal alignment inlatex. Default isFALSE. You will probably want to usedcolumnif you userdec, as a column may then contain varying number of places to the right of the decimal.dcolumncan line up all such numbers on the decimal point, with integer values right justified at the decimal point location of numbers that actually contain decimal places. When you usedcolumn = TRUE,numeric.dollaris set by default toFALSE. When you usedcolumn = TRUE, the object attribute"style"set to dcolumn as thelatexusepackagemust reference[dcolumn]. The three filesdcolumn.sty,newarray.sty, andarray.stywill need to be in a directory in yourTEXINPUTSpath. When you usedcolumn=TRUE,numeric.dollarshould be set toFALSE.- numeric.dollar
logical, default
!dcolumn. Set toTRUEto place dollar signs around numeric values whendcolumn = FALSE. This assures thatlatexwill use minus signs rather than hyphens to indicate negative numbers. Set toFALSEwhendcolumn = TRUE, asdcolumn.styautomatically uses minus signs.- math.row.names
logical, set true to place dollar signs around the row names.
- already.math.row.names
set to
TRUEto prevent any math mode changes to row names- math.col.names
logical, set true to place dollar signs around the column names.
- already.math.col.names
set to
TRUEto prevent any math mode changes to column names- na.dot
Set to
TRUEto use periods rather thanNAfor missing numeric values. This works with the SAS convention that periods indicate missing values.- blank.dot
Set to
TRUEto use periods rather than blanks for missing character values. This works with the SAS convention that periods indicate missing values.- col.just
Input vector
col.justmust have number of columns equal to number of columns of the output matrix. WhenNULL, the default, thecol.justattribute of the result is set to l for character columns and to r for numeric columns. The user can override the default by an argument vector whose length is equal to the number of columns of the result matrix. Whenformat.dfis called bylatex.default, thecol.justis used as thecolsargument to thetabularenvironment and the letters l, r, and c are valid values. Whenformat.dfis called by SAS, thecol.justis used to determine whether a \$ is needed on the input line of thesysinfile, and the letters l and r are valid values. You can pass specifications other thanl,r,cincol.just, e.g.,"p{3in}"to get paragraph-formatted columns fromlatex().- matrix.sep
When
xis a data frame containing a matrix, so that new column names are constructed from the name of the matrix object and the names of the individual columns of the matrix,matrix.sepspecifies the character to use to separate object names from individual column names.- scientific
specifies ranges of exponents (or a logical vector) specifying values not to convert to scientific notation. See
format.defaultfor details.- double.slash
should escaping backslashes be themselves escaped.
- format.Date
String used to format objects of the Date class.
- format.POSIXt
String used to format objects of the POSIXt class.
- ...
other arguments are accepted and passed to
format.default. ForlatexVerbatimthese arguments are passed to theprintfunction.
Value
a character matrix with character images of properly rounded x.
Matrix components of input x are now just sets of columns of
character matrix.
Object attribute"col.just" repeats the value of the argument col.just when provided,
otherwise, it includes the recommended justification for columns of output.
See the discussion of the argument col.just.
The default justification is l for characters and factors,
r for numeric.
When dcolumn==TRUE, numerics will have . as the justification character.
Author
Frank E. Harrell, Jr.,
Department of Biostatistics,
Vanderbilt University,
fh@fharrell.com
Richard M. Heiberger,
Department of Statistics,
Temple University, Philadelphia, PA.
rmh@temple.edu
Examples
if (FALSE) { # \dontrun{
x <- data.frame(a=1:2, b=3:4)
x$m <- 10000*matrix(5:8,nrow=2)
names(x)
dim(x)
x
format.df(x, big.mark=",")
dim(format.df(x))
} # }