Reputation: 113
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.
Upvotes: 10
Views: 18015
Reputation: 30124
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
Upvotes: 11
Reputation: 28632
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?
Upvotes: 1