Format all decimal places in R

How can I indicate, using a single command, the number of digits displayed during the entire R session? That is, how can I get the value 0 always display as 0.0 ? I tried the options(digits=1) command, but 0 still displays as 0 , not 0.0 . I'm really trying to avoid wrapping every command with something like print(ifelse(x==0,"0.0",x)) .

It would be nice if the solution to this problem, for example, 5 displayed as 5.0 .

+7
source share
2 answers

I'm not sure if there are options for trading 0. One of the possibilities is to have your own functions print.numeric and print.integer.

 print.integer <- print.numeric <- function(..., digs=1) { print(format(as.numeric(...), nsmall=digs), quote=F) } 

It still requires print , but ahead of

 > print(-1:5) [1] -1.0 0.0 1.0 2.0 3.0 4.0 5.0 


Alternatively, you can directly use the nsmall argument in the format.
 mat <- matrix(as.numeric(rep(0:3, 5)), ncol=4) print(format(mat, nsmall=2), quote=F) 
+6
source

You can try my formattable package.

 > # devtools::install_github("renkun-ken/formattable") > library(formattable) > n <- formattable(0, format = "f", digits = 1) > n [1] 0.0 

n is a vector formattable, numeric , which preserves its formatting in arithmetic calculations.

 > n+5 [1] 5.0 
0
source

All Articles