# Nhập dữ liệu từ tệp Excel
library(readxl)
pci <- read_excel("C:/dl/data/R-Rstudio/pci.xlsx", 
    col_types = c("text", "text", "text", 
        "numeric", "text", "text", "text"))
View(pci)
head(pci)
## # A tibble: 6 × 7
##   `Số PCI` PCIk  POBA  slstent DES   SuccessPCI TOCA 
##   <chr>    <chr> <chr>   <dbl> <chr> <chr>      <chr>
## 1 7601     1     1           1 1     1          1    
## 2 7620     1     0           1 1     1          0    
## 3 7644     1     1           0 0     1          1    
## 4 7648     1     0           1 1     1          1    
## 5 7669     1     0           1 1     1          0    
## 6 7673     1     0           1 1     1          1
library(gtsummary)
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Chuyển đổi biến TOCA thành yếu tố (factor) với các nhãn tương ứng
pci <- pci %>%
  mutate(TOCA = factor(TOCA, levels = c(0, 1), labels = c("không tắc hoàn toàn", "tắc hoàn toàn")))

# Tạo bảng tóm tắt đặc điểm PCI của dân số nghiên cứu
library(gtsummary)
library(ggplot2)
library(dplyr)

summary2 <- pci %>%
  select(PCIk, POBA, DES, SuccessPCI, TOCA) %>%
  tbl_summary(
    by = TOCA,
    type = all_continuous() ~ "continuous2",
    statistic = list(
      all_continuous() ~ c("{mean} ({sd})", "{median} ({p25}, {p75})", "{min}, {max}"),
      all_categorical() ~ "{n} / {N} ({p}%)"
    ),
    digits = all_continuous() ~ 2,
    label = list(
      PCIk ~ "Bệnh nhân được can thiệp ĐMV qua da",
      POBA ~ "Bệnh nhân được POBA",
      DES ~ "Bệnh nhân được đặt stent phủ thuốc",
      SuccessPCI ~ "Can thiệp thành công"),
    missing_text = "(Thiếu)"
  ) %>%
  add_p(test = list(
    PCIk ~ "chisq.test",
    POBA ~ "chisq.test",
    DES ~ "chisq.test",
    SuccessPCI ~ "chisq.test"
  ),
  pvalue_fun = ~ style_pvalue(.x, digits = 2)) %>%
  add_overall() %>%
  add_n() %>%
  modify_caption("Đặc điểm chụp mạch vành của dân số nghiên cứu")
## The following warnings were returned during `modify_caption()`:
## ! For variable `DES` (`TOCA`) and "statistic", "p.value", and "parameter"
##   statistics: Chi-squared approximation may be incorrect
## ! For variable `PCIk` (`TOCA`) and "statistic", "p.value", and "parameter"
##   statistics: Chi-squared approximation may be incorrect
## ! For variable `POBA` (`TOCA`) and "statistic", "p.value", and "parameter"
##   statistics: Chi-squared approximation may be incorrect
## ! For variable `SuccessPCI` (`TOCA`) and "statistic", "p.value", and
##   "parameter" statistics: Chi-squared approximation may be incorrect
# Xem kết quả
summary2
Đặc điểm chụp mạch vành của dân số nghiên cứu
Characteristic N Overall
N = 158
1
không tắc hoàn toàn
N = 120
1
tắc hoàn toàn
N = 38
1
p-value2
Bệnh nhân được can thiệp ĐMV qua da 158


0.46
    0
7 / 158 (4.4%) 4 / 120 (3.3%) 3 / 38 (7.9%)
    1
151 / 158 (96%) 116 / 120 (97%) 35 / 38 (92%)
Bệnh nhân được POBA 156


0.065
    0
147 / 156 (94%) 114 / 118 (97%) 33 / 38 (87%)
    1
9 / 156 (5.8%) 4 / 118 (3.4%) 5 / 38 (13%)
    (Thiếu)
2 2 0
Bệnh nhân được đặt stent phủ thuốc 152


0.49
    0
4 / 152 (2.6%) 2 / 117 (1.7%) 2 / 35 (5.7%)
    1
148 / 152 (97%) 115 / 117 (98%) 33 / 35 (94%)
    (Thiếu)
6 3 3
Can thiệp thành công 151


>0.99
    0
2 / 151 (1.3%) 2 / 116 (1.7%) 0 / 35 (0%)
    1
149 / 151 (99%) 114 / 116 (98%) 35 / 35 (100%)
    (Thiếu)
7 4 3
1 n / N (%)
2 Pearson’s Chi-squared test