`sjPlot::tab_df()`--如何设置小数位数?

`sjPlot::tab_df()`--how to set the number of decimal places?

我正在尝试使用 sjPlot 生成 table 的描述性统计数据,并且我正在尝试设置小数点后的位数。

这是我尝试过的方法(下面的可重现示例):

library(sjPlot)
library(sjmisc)

tab_df(mtcars, digits = 1)

descr(mtcars) %>% 
  tab_df(digits = 1)

在这两种情况下,小数点后仍然有很多数字。

编辑:

下面 Daniel 的代码生成的结果接近我正在寻找的结果,但您仍然可以看到尾随零被四舍五入,因此并非所有数字都保留两位小数。

library(sjPlot)
library(sjmisc)

tab_df(mtcars, digits = 1)

descr(mtcars) %>% 
  tab_df(digits = 1)

一个快速的解决方法是使用 sjmisc::round_num():

library(sjPlot)
library(sjmisc)

tab_df(mtcars, digits = 1)

descr(mtcars) %>% 
  round_num(2) %>% 
  tab_df()