I have the following dataframe with tableby.control function and its parameters as follows :
---
title: "Title"
subtitle: "Descriptive analysis"
author:
- name: AA
date: last-modified
format:
html:
page-layout: full
code-fold: false
code-tools: false
embed-resources: true
self-contained: true
self-contained-math: true
df-print: paged
toc: false
toc-depth: 3
toc-location: right
# pdf:
# toc: true
# number-depth: 3
docx:
prefer-html: true
echo: false
warning: false
toc: false
reference-doc: 04-Question.docx
format-links: false
editor: visual
---
{r, results='asis', echo = FALSE}
library(knitr)
library(kableExtra)
library(dplyr)
library(tidyr)
library(labelled)
library(ggplot2)
library(scales)
library(arsenal)
library(survival)
library(survminer)
library(parameters)
df = data.frame(
id = 1:12,
Groups = c(rep("ARM A",5),rep("ARM B",7)),
procedure = c(rep(c("Yes","No"),5),"No","No"),
If_yes_Biopsy = c(rep(c("Yes","No"),5),"No","No"),
If_yes_Dissection = c("Yes","No","Yes","No","No","No","No","No","No","No","No","No"),
Was_there_pain = rep(c("Yes","No"),6),
If_yes_Abdomen = c("Yes","No","No","No","Yes","No","Yes","No","No","No","Yes","No"),
If_yes_Leg = c("Yes","No","Yes","No","No","No","No","No","Yes","No","No","No")
)
mycontrols <- tableby.control(test=TRUE,
total=TRUE,
numeric.test="kwt", cat.test="chisq",
numeric.stats=c("N", "Nmiss2", "meansd", "medianq1q3", "range"),
cat.stats=c("countpct", "Nmiss2"),
stats.labels=list(Nmiss2 = "Missing",
meansd = "Mean (SD)",
medianq1q3 = "Median (Q1, Q3)",
range = "Min - Max"))
tableby(Groups ~
procedure +
If_yes_Biopsy +
If_yes_Dissection +
Was_there_pain +
If_yes_Abdomen +
If_yes_Leg,
data = df, control = mycontrols) %>%
summary(pfootnote = TRUE, digits=1, digits.pct=1, digits.p=2)
When running the code using Quarto, I have the following output :
I wish that the percentages in variables that start with “if_yes” to take into account the variable before while keeping the same format in table as in the desired outcome
ARM A (N=5) ARM B (N=7) Total (N=12) p value
procedure
No 2 (40.0%) 5 (71.4%) 7 (58.3%)
Yes 3 (60.0%) 2 (28.6%) 5 (41.7%)
Missing 0 0 0
If_yes_Biopsy
No 0 (0.0%) 0 (0.0%) 0 (0.0%)
Yes 3 (100.0%) 2 (100.6%) 5 (100%)
Missing 2 5 7
If_yes_Dissection
No 1 (33.3%) 2 (100.0%) 3 (33.3%)
Yes 2 (66.7%) 0 (0.0%) 2 (66.7%)
Missing 2 5 7
Was_there_pain
No 2 (40.0%) 4 (57.1%) 6 (50.0%)
Yes 3 (60.0%) 3 (42.9%) 6 (50.0%)
Missing 0 0 0
If_yes_Abdomen
No 1 (33.3%) 1 (33.3%) 2 (33.3%)
Yes 2 (66.7%) 2 (66.7%) 4 (66.7%)
Missing 2 4 6
If_yes_Leg
No 1 (33.3%) 2 (66.7%) 3 (50.0%)
Yes 2 (66.7%) 1 (33.3%) 3 (50.0%)
Missing 2 4 6