0

here I have a data frame with the following varible age gender disease and stage

# create simulated data
set.seed(123)
n <- 20
data <- data.frame(
  age = rnorm(n, mean = 50, sd = 10),
  gender = sample(c("Male", "Female"), n, replace = TRUE),
  disease = sample(c("Yes", "No"), n, replace = TRUE),
  stage = sample(0:2, n, replace = TRUE)
)

I want to present the data in a table with mean(SD) of age for each group or vriable

if I want to presnt it in table I could grouped by gender and presented like this

# create gtsummary table
table <- data %>%
  tbl_summary(
    by = gender, 
    statistic = all_continuous() ~ "{mean} ({sd})", 
    missing = "no",
    digits = all_continuous() ~ 2) %>%
  add_p(test = everything() ~ "t.test", 
        test.args = all_tests("t.test") ~ list(var.equal = TRUE)) %>%
  bold_labels()
  
# print table
table

but I want to present it by age so it looks like this

varibel Age Gender

varible Age Mean (SD) P-value
Gender < 0.001
Male 32 (4)
Female 23 (3)
disease 0.061
Yes 26 (5)
No 28 (4)

I tried this but it didn't work

# create gtsummary table
table <- data %>%
  tbl_summary(
    by = c(gender, disease, stage), 
    statistic = all_continuous() ~ "{mean} ({sd})", 
    missing = "no",
    digits = all_continuous() ~ 2) %>%
  add_p(test = everything() ~ "t.test", 
        test.args = all_tests("t.test") ~ list(var.equal = TRUE)) %>%
  bold_labels()
  
# print table
table

it dosent matter what the library is gtsummary or anything I just want a table to extract to Word

0 Answers0