Re: [R] numeric format

From: Rolf Turner <r.turner_at_auckland.ac.nz>
Date: Wed, 27 Feb 2008 11:23:32 +1300

On 27/02/2008, at 11:01 AM, John Kane wrote:

> Can you give a working example of what is happening
> and explain what is x?
>
> With a simple x vector of x <- rnorm(20, 5, 2)
> I don't get anything like what you seem to be getting.
>
> My code
> ===================================================
> x <- rnorm(20, 5, 2)
> table<-data.frame(x, scientific=F, digits=4)
> table
> ===================================================
>
> The numbers on the left are simply line numbers that
> are automatically printed when you are printing a
> dataframe to the screen. I don't see any way to
> supress them for a simple command such as your
> table
>
> You might want to have a look at print and
> print.default to address the digits problem

        <snip>

I have often wanted to suppress these row numbers and for that purpose wrote the following version of print.data.frame() which I keep in a local package (that I have set up to be loaded automatically on startup) which masks the system version 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)
         prmatrix(format(x), rowlab = rowlab, ..., quote = quote,
             right = right)
     }
     invisible(x)

}

The ``srn'' argument means ``suppress row numbers''; it defaults to FALSE so by default
the behaviour of my print.data.frame() is the same as that of the system print.data.frame().
To suppress the row numbers you can say, e.g.

        print(junk,srn=TRUE)

Note to Young Players --- you need only type ``print'' in the above, r.t. ``print.data.frame'',
since print.data.frame() is a ``method'' for print().

I once suggested to an R Core person that my version of print.data.frame() be adopted as the
system version, but was politely declined.

                        cheers,

                                Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 26 Feb 2008 - 22:25:35 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 27 Feb 2008 - 23:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive