In the field of psychology, it is customary to remove a 0 before the decimal when the value above 1.0 is impossible.
Therefore, I'd like to change p values from, for example:
p = 0.01 to p = .01
Additionally, when making a regression table using tbl_regression
,
I'd like to change standardized beta coefficients to, for example:
beta = -0.43 to beta = -.43 (in the regression table)
Or, R^2:
R^2 = 0.234 to R^2 = .234 (through
add_glance_statistics
)
Is there a way to do this? I have looked at style tools, but there does not seem to be a configuration for this.
CodePudding user response:
You can use modify_fmt_fun
to modify the format of the table. Here, I use trial
data from gtsummary
to show how you can remove leading 0s for p.value
. After applying sub
to remove the leading 0, it returns NA for the other rows. So, I use gsub
to change the NA back to blanks for the table.
library(gtsummary)
library(tidyverse)
trial[c("age", "grade", "trt")] %>%
tbl_summary(by = trt) %>%
add_p() %>%
modify_fmt_fun(update = p.value ~ function(val) {
as.character(sub("^(-?)0.", "\\1.", sprintf("%.2f", val))) %>%
gsub("NA", "", .)
})
Output
If you need to do the same on others, then you can add in additional parameters. Here, I add in beta (i.e., estimate) to modify_fmt_fun
.
lm(age ~ marker grade, trial) %>%
tbl_regression() %>%
add_glance_source_note(
label = list(df ~ "Degrees of Freedom", sigma ~ "\U03C3"),
fmt_fun = df ~ style_number,
include = c(r.squared, AIC, sigma, df)
) %>%
modify_fmt_fun(update = c(estimate, p.value) ~ function(val) {
as.character(sub("^(-?)0.", "\\1.", sprintf("%.2f", val))) %>%
gsub("NA", "", .)
})
Output