olveraa
olveraa

Reputation: 35

Showing p-values in scientific notation with gtsummary::tbl_regression?

Your help is most appreciated to include p-values for gtsummary::tbl_regression function in scientific notation. I am using big data and it would be really useful to show 3 decimal points for p-values > 0.001 and scientific notation for p-values <0.001.

A representative example

# load packages
library(gtsummary)
theme_gtsummary_compact()

# linear model 
mod1 <- lm(mpg ~ cyl + gear, data = mtcars %>% dplyr::mutate(cyl = factor(cyl)))

# regression table 
tbl_regression(mod1)

I wonder if it is possible to get the 2.06e-4, 5.76e-8, and 0.422 p-value notations for cyl 6, cyl 8, and gear, respectively.

summary(mod1)

Thanks very much!

Upvotes: 2

Views: 1031

Answers (1)

Harrison Jones
Harrison Jones

Reputation: 2506

You can define your own function for the p-value format:

tbl_regression(
  mod1, 
  pvalue_fun = function(x) {
    if_else(
      is.na(x), 
      NA_character_,
      if_else(x < 0.001, format(x, digits = 3, scientific = TRUE), format(round(x, 3), scientific = F))
    )
  } 
)

Upvotes: 4

Related Questions