[R] Suppressing row labels.

Rolf Turner rolf at math.unb.ca
Thu Mar 6 20:42:51 CET 2003


Very often when I print a data frame (particularly when sink()-ing to
a file I do NOT want the row labels (which are in such cases usually
1, 2, ... nrow(x), where x is the data frame in question).  I can of
course edit these out ``by hand'', but that's a bit of a pain in the
pohutukawa.

I recently discovered (reading the help on print.data.frame
meticulously, and following it through to print.matrix) that
I CAN suppress these row names by executing, e.g.

	> print(x,rowlab=rep("",nrow(x)))

However it would be ever-so-slightly nicer (require somewhat fewer
key strokes) if there were an option to print.data.frame that would
allow suppression of row names.  E.g. an argument ``srn=FALSE''.
(Where ``srn'' stands for ``suppress row names''.)

I have written my own simple minded implementation of this option,
given below in case anyone else out there may have shared my
frustration at being unable to figure out how to suppress row names.

I would ask that the R developers consider including an argument like
unto this in a future release.

On a related note:  I would also like, generally speaking, NOT to
have row names included in LaTeX tables that I create from data
frames or matrices.  As far as I can discern, none of the currently
available LaTeX-ing functions provide the option of suppressing the
row names.  The functions I've looked at are:

	o xtable{xtable}
	o latex.table{quantreg}
	o latex{Hmisc}

It would be nice (said he wistfully) if the developers of at
least one of these packages would consider adding such an option.


					cheers,

						Rolf Turner

===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===
Code for my mild revision of print.data.frame:
===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===
print.data.frame <- function (x, ..., digits = NULL, quote = FALSE,
                              right = TRUE, srn=FALSE)
{
    if (length(x) == 0) {
        cat("NULL data frame with", length(row.names(x)), "rows\n")
    }
    else if (length(row.names(x)) == 0) {
        print.default(names(x), quote = FALSE)
        cat("<0 rows> (or 0-length row.names)\n")
    }
    else {
        if (!is.null(digits)) {
            op <- options(digits = digits)
            on.exit(options(op))
        }
        rowlab <- if(srn) rep("",nrow(x)) else row.names(x)
        print.matrix(format(x), rowlab=rowlab, ..., quote = quote,
                     right = right)
    }
    invisible(x)
}
===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===



More information about the R-help mailing list