R - Vary by Row the Rounding of Digits Produced by knitr::kable

后端 未结 2 1358
余生分开走
余生分开走 2021-02-10 16:27

I am trying to produce a markdown table using knitr::kable where the rounding of digits varies by rows. For example, suppose I have the following data.frame:

exa         


        
2条回答
  •  礼貌的吻别
    2021-02-10 17:03

    As pointed out by @user20650, one solution consists in converting the format of the entries in your dataframe in R before passing the data to knitr. It could be done like 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 the following output:

    > kable(example.df, align="r")
    
    |    a|    b|
    |----:|----:|
    | 0.12| 0.57|
    | 2000| 3000|
    

提交回复
热议问题