简体   繁体   中英

How to create a function to display the 25th and 75th percentile (IQR) in table1 package

For continuous variables in my dataframe, I want to use the table1 package in R to get a table showing median (25th-75th percentile) as output for each continuous variable.

Using the method described by Benjamin Rich in a table1 vignette:

my.render.cont <- function(x) {
    with(stats.apply.rounding(stats.default(x), digits=2), c("",
        "Mean (SD)"=sprintf("%s (&plusmn; %s)", MEAN, SD)))
}

How do I change this to instead of showing the data like Median (+/- IQR) to show it like Median (25th percentile - 75th percentile)?

If a reproducible example is needed:

library(table1)
library(MASS)

labels <- list(variables=list(age="Age (years)",thickness="Thickness (mm)"),groups=list("", "", ""))
strata <- split(Melanoma, Melanoma$status)
 
my.render.cont <- function(x) {
    with(stats.apply.rounding(stats.default(x), digits=2), c("",
        "Mean (SD)"=sprintf("%s (&plusmn; %s)", MEAN, SD)))
}

table1(strata, labels, render.continuous=my.render.cont)

Try this:

library(table1)
library(MASS)

labels <- list(variables=list(age="Age (years)",thickness="Thickness (mm)"),groups=list("", "", ""))
strata <- split(Melanoma, Melanoma$status)

my.render.cont <- function(x) {
  with(stats.apply.rounding(stats.default(x, ), digits = 2),
       c("",
         "median (Q1-Q3)" =
           sprintf(paste("%s (",Q1,"- %s)"), MEDIAN,Q3)))
}

table1(strata, labels, render.continuous=my.render.cont)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM