R - Vary by Row the Rounding of Digits Produced by knitr::kable -
i trying produce markdown table using knitr::kable rounding of digits varies rows. example, suppose have following data.frame:
example.df <- data.frame(a = c(0.1234, 2000), b = c(0.5678, 3000))
i'd create following markdown table, first row rounded 2 digits , second row rounded integer.
| a| b| |----:|----:| | 0.12| 0.57| | 2000| 3000|
instead, seem able format entire columns.
library(knitr) kable(example.df, digits = c(2, 0))
which results in following markdown table:
| a| b| |-------:|----:| | 0.12| 1| | 2000.00| 3000|
any advice on how produce markdown table in r has row-wise formatting of digits?
as pointed out @user20650, 1 solution consists in converting format of entries in dataframe in r
before passing data knitr
. done this:
example.df <- rbind(formatc(as.numeric(example.df[1,]),format="f",digits=2), formatc(as.numeric(example.df[2,]),format="d")) colnames(example.df) <- c("a","b")
this gives following output:
> kable(example.df, align="r") | a| b| |----:|----:| | 0.12| 0.57| | 2000| 3000|
Comments
Post a Comment