[R] digits argument for print method for dataframe

David Kane <David Kane a296180 at mica.fmr.com
Tue Jun 12 13:21:36 CEST 2001

I have two questions:

First, how does the digits argument to the print method for dataframes work? I
wandered around through print.data.frame and format.data.frame without fully
understanding what is going on.
Here is a simple example:

> version
platform sparc-sun-solaris2.6
arch     sparc               
os       solaris2.6          
system   sparc, solaris2.6   
major    1                   
minor    2.3                 
year     2001                
month    04                  
day      26                  
language R                   
> set.seed(9)
> dum <- data.frame(w = rnorm(3), x = rnorm(3) + 100, y  = rnorm(3))
> dum
          w         x           y
1  1.154443  98.71592 -0.09822585
2 -1.190371  99.36611 -0.56244518
3 -1.444471 102.28006 -1.45055500
> options("digits")
[1] 7

Why are there 6 digits after the decimal for "w" but 8 for "y"? My guess, after
some experimentation, is that a) each column is treated independently (seems
clear enough in the code) and b) the number of digits after the decimal is set
so that every number in that column has a least "digits" numerals (after any
leading zeroes). So, in "w", we need 6 numbers after the decimal (plus the
leading 1 or -1) to get us to 7 digits while for "y" we need 8 digits so that
the first value (0.09822585) can have 7 "real" digits after it's leading
-0.0. Similarly, with x we need 5 digits after the decimal so that the first
two values will have a total of seven digits (when we include the 98/99). Is
this correct and can this behavior be overridded? For example, I would like to
be able to have a column have two digits after the decimal regardless of any
leading zeroes. As best I can tell this is not possible. Also, I would like to
be able to format different columns differently (i.e., pass in a vector to the
digits argument). I don't think that this is possible, but am I missing something?

> print(dum, digits = 3)
      w     x       y
1  1.15  98.7 -0.0982
2 -1.19  99.4 -0.5624
3 -1.44 102.3 -1.4506
> print(dum, digits = c(3,10,10))
      w     x       y
1  1.15  98.7 -0.0982
2 -1.19  99.4 -0.5624
3 -1.44 102.3 -1.4506

Second, do people have generic advice on "pretty printing"? My needs are
limmitted (control character justification, control the number of digits after
the decimal, place commas in large numbers). I can think of at
least 3 ways to go. First, dump the data into Perl and use it. Second, write
code (actually steal complex code from a friend) that creates Latex
tables. Third (and this is hazy in my find), create my own format methods that
do what I want. Surely, other people have confronted this problem before . . .

As a side note, it seems like the help for format (?format) has left out a sentence fragment:

     `format.data.frame' formats the data frame column by column,
     applying the appropriate method of `format' for each column. The
     result is a data frame, so in most cases



Thanks for any advice or pointers,

Dave Kane

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list