#To prepare a table one for the blood storage data set from medical data. #Connection: Chapter_3 on Rpubs

#install.packages("gtsummary")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.2     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(medicaldata)
library(gt)
library(gtsummary)
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
view(blood_storage)

#clean column names

prostate <- medicaldata::blood_storage |> 
  clean_names()
view(prostate)

#Now to make the table.

prostate |>
  tbl_summary(include = c(age, p_vol, preop_psa, aa, fam_hx),
              statistic = list(all_categorical() ~ "n = {n}"),
              digits = all_continuous() ~ 0)
Characteristic N = 3161
age 62 (56, 66)
p_vol 49 (41, 64)
    Unknown 9
preop_psa 6 (5, 9)
    Unknown 3
aa n = 55
fam_hx n = 68
1 Median (Q1, Q3); n = n

#Now to make a better table

prostate |> 
  tbl_summary(
    include = c(age, p_vol, preop_psa, fam_hx),
    statistic = list(all_categorical() ~ "{n}",
                     all_continuous() ~ "{mean} ({sd})"),
    label = list(preop_psa ~"Preop-PSA",
                 age ~ "Age",
                 p_vol ~ "Prostate volume",
                 fam_hx ~ "Family history")
  )
Characteristic N = 3161
Age 61 (7)
Prostate volume 56 (30)
    Unknown 9
Preop-PSA 8.2 (6.0)
    Unknown 3
Family history 68
1 Mean (SD); n