Search code examples
rsjplot

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


I'm trying to use sjPlot to produce a table of Descriptive Statistics, and I am trying to set the number of digits after the decimal point.

This is what I've tried (reproducible example below):

library(sjPlot)
library(sjmisc)

tab_df(mtcars, digits = 1)

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

In both cases, I'm still getting many digits trailing after the decimal point.

enter image description here

Edit:

The code below from Daniel produces close to the result I'm looking for, but you can still see that trailing zeros are being rounded off, so not all numbers are to two decimal places.

library(sjPlot)
library(sjmisc)

tab_df(mtcars, digits = 1)

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

enter image description here


Solution

  • A quick workaround would be using sjmisc::round_num():

    library(sjPlot)
    library(sjmisc)
    
    tab_df(mtcars, digits = 1)
    
    descr(mtcars) %>% 
      round_num(2) %>% 
      tab_df()