0

Thanks to the creator of the modelsummary package from a newbie.

I'm calling datasummary_df to print descriptive statistics in an RMarkdown table, and would like no significant digits in some columns and two significant digits in others. Datasummary_df has only a single fmt parameter, but this helpful question has a solution for that when calling modelsummary functions on models. I'm not sure how to apply that answer when calling the function that just prints a dataframe (datasummary_df) instead of working with models.

I'm also hoping to allow the column titles to wrap, but am unsure how to do that.

Can anyone point me in the right direction? Many thanks.

1 Answers1

1

modelsummary::datasummary_df is just using the R package kableExtra internally. We can use dplyr verbs to round particular columns before plotting the table:

library(tidyverse)
library(kableExtra)

set.seed(1337)
data <- tibble(
  A =  runif(5),
  B =  runif(5),
  C =  runif(5)
)

data %>%
  mutate(
    A = A %>% round(),
    B = B %>% round(2),
    C = C %>% round(2)
  ) %>%
  kable() %>%
  kable_styling()

enter image description here

round(2) can be replaced by sprintf(fmt = "%.2f") to show two digits after the decimal point without prior rounding. This, however, will convert the numbers to text.

danlooo
  • 10,067
  • 2
  • 8
  • 22