kable displays different number of digits in each column

user3866200 picture user3866200 · Jul 22, 2014 · Viewed 10.2k times · Source

I am using kable with the knit to Word functionality at work. I find that I often have simple tables with counts as the first column and then a few columns with proportions. I'd like the count column to be rounded to the nearest digit and the other columns to the nearest hundredth. I've tried using the digits = c(0,2,2) argument within the kable() command, but it still displays two digits for the count, even though it is rounding to the nearest digit.

Answer

Yihui Xie picture Yihui Xie · Jul 26, 2014

I do not see the problem here.

> knitr::kable(as.data.frame(matrix(rnorm(12), 4)), digits = c(0, 2, 2))


| V1|    V2|    V3|
|--:|-----:|-----:|
| -1|  2.11| -0.54|
|  0| -0.33|  0.95|
| -1| -1.14| -0.96|
|  0|  1.45| -0.93|
> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] evaluate_0.5.5 formatR_0.10.5 knitr_1.6   stringr_0.6.2  tools_3.1.1