Pander the number of digits after the decimal point

I am trying to output a table using pander in a .rmd file as pdf with 2 digits after the decimal point, but I am not getting digits using the following rmd:

--- title: "Long table test" output: pdf_document --- Here is a table: ```{r setup} library (data.table) library (pander) set.seed(1984) longString <- "description string" dt <- data.table(id=c(1:3),description=rep(longString,3),value=rnorm(3,mean=10000,sd=1)) ``` ```{r pander-table} panderOptions('round',2) panderOptions('digits',2) panderOptions('keep.trailing.zeros',TRUE) pander(dt, split.cell = 80, split.table = Inf) ``` 

leads to

 ------------------------------- id description value ---- ------------------ ------- 1 description string 10000 2 description string 10000 3 description string 10001 ------------------------------- 

I would like to see

 ---------------------------------- id description value ---- ------------------ ---------- 1 description string 10000.41 2 description string 9999.68 3 description string 10000.64 ---------------------------------- 
+2
source share
2 answers

The ?panderOptions page indicates that the “digits” are passed in format , where it is interpreted as the number of “significant digits”. Significant numbers are really very little associated with decimal places. You can have 2 significant digits in decimal value of 0.000041. You can see the effect of your parameter on the format() -ed values:

 > format(c( 10000.41, 9999.68, 10000.64 ), digits=2) [1] "10000" "10000" "10001" 

You want to keep the round option at 2.

0
source

Setting round does not directly affect the number of digits (although some indirect effect is associated with a potential return of digits of a negligible ( 0 )). The main problem here is that pander does not allow you to set the nsmall format() parameter, which would set

minimum number of digits to the right of the decimal point when formatting real / complex numbers in non-scientific formats. Allowed values: 0 <= nsmall <= 20.

But since pander only feeds numeric values ​​to format() , you can just work around this by supplying the values ​​of as.character() to pander:

 library (data.table) library(magrittr) library (pander) set.seed(1984) longString <- "description string" dt <- data.table(id = c(1:3), description = rep(longString, 3), value = rnorm(3, mean = 10000, sd = 1)) pander( x = dt %>% mutate(value = value %>% round(2) %>% as.character()), split.cell = 80, split.table = Inf, justify = "ccr" ) 

that leads to:

 ------------------------------------ id description value ---- -------------------- ---------- 1 description string 10000.41 2 description string 9999.68 3 description string 10000.64 ------------------------------------ 
0
source

Source: https://habr.com/ru/post/958785/


All Articles