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.
Asked
Active
Viewed 1.6k times
10
-
2Can you please provide a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – Thomas Jul 22 '14 at 20:14
-
Can you convert the counts to `integer` type? Does that make any difference at all when printing? – talat Jul 22 '14 at 20:29
2 Answers
11
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

Yihui Xie
- 28,913
- 23
- 193
- 419
-
Thank you!! I did not have as.data.frame at the beginning. Adding that has fixed it. So simple. – user3866200 Aug 19 '14 at 18:43
1
A quick example with pander
:
> df <- data.frame(a = 1:5, b = runif(5), c = runif(5))
> library(pander)
> pander(df)
------------------
a b c
--- ------ -------
1 0.5949 0.4595
2 0.7645 0.5012
3 0.7755 0.6024
4 0.818 0.01271
5 0.4329 0.7588
------------------
> panderOptions('digits', 2)
> pander(df)
--------------
a b c
--- ---- -----
1 0.59 0.46
2 0.76 0.5
3 0.78 0.6
4 0.82 0.013
5 0.43 0.76
--------------
This is what you need?

daroczig
- 28,004
- 7
- 90
- 124