N <- 1000
toydf <- data.frame(
children = rbinom(N, size = 3, prob = 0.5),
age = extraDistr::rdunif(N, 10, 100),
gender = sample(c("M", "F"), N, replace = TRUE)
)
toydf$age %<>% as.integer()
toydf$gender %<>% as.factor()
my_mean_diff <- function(data, variable, by, tbl, ...) {
x <- data[[variable]]
g <- data[[by]]
lvls <- levels(g)
vartype <- class(x)[1]
# <UNCOMMENT THIS TO MAKE IT WORK>
#if(
# (vartype=="character" | vartype=="factor" | vartype=="numeric" | vartype=="integer") &
# length(unique(x)) <= 10
#)
#{
# vartype <- "categorical"
#}
print(paste0("Variable: ", variable, ", Type: ", vartype))
switch(
vartype,
categorical = {
prop <- prop.table(table(x, g), margin = 2)
return((prop[, lvls[2]] - prop[, lvls[1]]) * 100)
},
integer = {
return(diff(tapply(x, g, mean, na.rm = TRUE)))
},
{
stop(glue("ERROR: Unrecognized type {vartype}"))
}
)
}
toydf %>%
tbl_summary(
statistic = list(
all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} ({p}%)"
),
missing = "no",
by = "gender",
percent = "column"
) %>% add_stat(
fns = everything() ~ my_mean_diff,
location = list(
all_continuous() ~ "label",
all_categorical() ~ "level",
all_dichotomous() ~ "label"
)
)
Error in `add_stat()`:
! Dimension of "children" and the added statistic do not match.
ℹ Expecting statistic/data frame to be length/no. rows 4.
Run `rlang::last_trace()` to see where the error occurred.
Consider the following example:
It will return:
This is because, having
childrenfewer than 10 levels,gtsummaryis treating it as a factor variable.But at this point we should pass the variable to custom
add_statfunction as factor, not as numeric.