Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R options(digits=2) function changes the total number of digits’ format. Looking for a way to change the digits after decimal point

Tags:

r

The options(digits=2) changed the total number of digits going forward:

> options(digits=3)

> 1.234 

[1] 1.23
> 12.234 

[1] 12.2
> 123.234 

[1] 123

Is there any way to change the number formatting such that it changes the digits after decimal point, so with digits_after_decimal=3 the number 1.12345 becomes 1.123 and 123.12345 become 123.123?

I am aware of the functions round or format; however, they just return the result based on parameters whereas options(digits=3) changes all formatting going forward.

like image 396
Allan Xu Avatar asked Apr 03 '16 16:04

Allan Xu


1 Answers

Request for improved "reporting" options in R have a long history, but never have been met with modifications to base-R. It's not possible to change the behavior of the print.default function which is what you are using (without knowing so) to do rounding with the flip of an option switch. (You can check that there is no print.numeric by using methods.) So my advice would be to learn to use round, sprintf or formatC on the items you want rounded. There will be an additional "hitch" in that the sprintf or formatC results will be a character value and it will display with quotes unless you use cat to print, or set quote=FALSE for a print call. @digEmAll illustrated 2 of these issues with his suggestion. You may want to look at more "complete" strategies for getting reporting formatting as offered by the various "tables" functions and packages. You can see a wide array of such by searching on the available packages with "table" and "tabular". The ones I have experience with include htmlTables, tabular, and xtable but there are many others and this does not include the knitr and Sweave options.

like image 88
IRTFM Avatar answered Oct 10 '22 11:10

IRTFM