[R] Option to Print with numbers formatted

Duncan Murdoch murdoch.duncan at gmail.com
Fri Nov 19 16:19:45 CET 2010

On 19/11/2010 9:59 AM, Santosh Srinivas wrote:
> Hello group,
> I have a data frame called test.df  with a bunch of columns.
> When I do a print(test.df), I want the numbers to appear a pre-defined
> setting.
> I believe this can be achieved by sprintf but this needs to be done
> individually for the data.
> However, is there an option that I can set so that all numerical values
> default to comma separated, 2 decimal places when they appear?

No, there's no global option with that level of flexibility.  You can 
set options("digits"), but you won't get the commas.

You should probably write your own function to do this.  For example,

myformat <- function(x, big.mark=",", scientific = FALSE, nsmall=2, ...) {
    format.data.frame(x, big.mark=big.mark, scientific=scientific, 
nsmall=2, ...)

and then print(myformat(df)) should give what you want.  It is possible 
to replace the print.data.frame and format.data.frame functions with new 
ones, but that's probably not advisable.

Duncan Murdoch

More information about the R-help mailing list