#Insert data

library(readxl)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.2
## 
## 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
A <- read_excel("/Users/macair/Desktop/cf.xlsx")

#taobienmoi

bien_Saisot <- c(
  "Tram_thieu",
  "tram_thua",
  "Thung",
  "Gay_DC",
  "Go_Tat",
  "khac_ong",
  "ko_dongnhat"
)

#Thong ke mo ta ##Figure1.Distribution of patients by gender

library(dplyr)
A %>%
count(gioitinh)%>%
mutate(percent=round(n/sum(n)*100,1)
)
## # A tibble: 3 × 3
##   gioitinh     n percent
##   <chr>    <int>   <dbl>
## 1 0           17    28.3
## 2 1           35    58.3
## 3 <NA>         8    13.3

##Figure2.Distribution of patients by age group

 A%>%
count(nhom_tuoi)%>%
mutate(percent=round(n/sum(n)*100,1)
)
## # A tibble: 5 × 3
##   nhom_tuoi     n percent
##   <chr>     <int>   <dbl>
## 1 17–40        26    43.3
## 2 41–64        12    20  
## 3 Dưới 16       8    13.3
## 4 Trên 65       6    10  
## 5 <NA>          8    13.3

##Table 1. Patient Status

vars <- c("tien su", "Vo cam", "tam ly", "ha mieng", "Non", "tinh trang cap cuu")
bang1 <-lapply(vars, function(v){
A %>%
count(.data[[v]]) %>%
mutate(percent=round(n/sum(n)*100,1),
bien=v
)
})
bang1 <- bind_rows(bang1)

##Table 2.Clinical Characteristics of Endodontic Cases

vars <- c("chan doan", "Vi tri R", "do nghieng", "do xoay", "co lap", "Than_rang")
bang2 <-lapply(vars, function(v){
A %>%
count(.data[[v]]) %>%
mutate(percent=round(n/sum(n)*100,1),
)
})
bang2<- bind_rows(bang2)

##Table 3. Radiographic Characteristics of Endodontic Cases on Periapical Radiographs

vars3 <- c("Kho_chup", "XQ", "San_tuy", "KC", "Lo_chop", "Tieu_ngot","cong")
bang3 <-lapply(vars3, function(v){
A %>%
count(.data[[v]]) %>%
mutate(percent=round(n/sum(n)*100,1),
)
})
bang3<- bind_rows(bang3)

##Table 4. Endodontic Case Difficulty Levels According to the AAE

 A %>%
count(PL_dokho) %>%
mutate(percent=round(n/sum(n)*100,1)
)
## # A tibble: 2 × 3
##   PL_dokho     n percent
##   <chr>    <int>   <dbl>
## 1 Cao         48      80
## 2 Vừa         12      20

##Figure 3. Frequency of technical errors in root canal treatment (%)

figure3 <-lapply(bien_Saisot, function(v){
A %>%
count(.data[[v]]) %>%
mutate(percent=round(n/sum(n)*100,1),
)
})
figure3<-bind_rows(figure3)

##Table 5. Association between case difficulty and the occurrence of endodontic treatment

tab <- table(A$PL_dokho, A$Saisot)
tab
##      
##        0  1
##   Cao 23 25
##   Vừa 12  0
fisher.test(tab)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  tab
## p-value = 0.0007115
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
##  0.0000000 0.3864994
## sample estimates:
## odds ratio 
##          0

##Table 6. Association between case difficulty and specific technical errors in endodontic treatment

ketqua_fisher <- lapply(bien_Saisot, function(v) {

  tab6 <- table(A$PL_dokho, A[[v]])
  pct <- round(prop.table(tab6, 1) * 100, 1)
  p <- fisher.test(tab6)$p.value

  data.frame(
    Bien = v,
    Cao_n   = ifelse("1" %in% colnames(tab6), tab6["Cao", "1"], 0),
    Cao_pct = ifelse("1" %in% colnames(tab6), pct["Cao", "1"], 0),
    Vua_n   = ifelse("1" %in% colnames(tab6), tab6["Vừa", "1"], 0),
    Vua_pct = ifelse("1" %in% colnames(tab6), pct["Vừa", "1"], 0),
    p_value = round(p, 4)
  )
})

ketqua_fisher <- do.call(rbind, ketqua_fisher)
ketqua_fisher
##          Bien Cao_n Cao_pct Vua_n Vua_pct p_value
## 1  Tram_thieu     9    18.8     0       0  0.1823
## 2   tram_thua    11    22.9     0       0  0.0994
## 3       Thung     3     6.2     0       0  1.0000
## 4      Gay_DC     4     8.3     0       0  0.5744
## 5      Go_Tat     6    12.5     0       0  0.3331
## 6    khac_ong     9    18.8     0       0  0.1823
## 7 ko_dongnhat     6    12.5     0       0  0.3331