R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

df = read.csv("D:\\TAM DAN NON-ORTHO\\06. Non ortho_OCCLUSION\\06.1 Non ortho_IOTN\\06.1 Non ortho_IOTN.csv")
library("lessR")
## Warning: package 'lessR' was built under R version 4.5.2
## 
## lessR 4.5                            feedback: gerbing@pdx.edu 
## --------------------------------------------------------------
## > d <- Read("")  Read data file, many formats available, e.g., Excel
##   d is the default data frame, data= in analysis routines optional
## 
## Many examples of reading, writing, and manipulating data, graphics,
## testing means and proportions, regression, factor analysis,
## customization, forecasting, and aggregation to pivot tables.
##   Enter: browseVignettes("lessR")
## 
## View lessR updates, now including modern time series forecasting
##   and many, new Plotly interactive visualizations output. Most
##   visualization functions are now reorganized to three functions:
##      Chart(): type="bar", "pie", "radar", "bubble", "treemap", "icicle"
##      X(): type="histogram", "density", "vbs" and more
##      XY(): type="scatter" for a scatterplot, or "contour", "smooth"
##    Most previous function calls still work, such as:
##      BarChart(), Histogram, and Plot().
##   Enter: news(package="lessR"), or ?Chart, ?X, or ?XY
## There is also Flows() for Sankey flow diagrams, see ?Flows
## 
## Interactive data analysis for constructing visualizations.
##   Enter: interact()
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:lessR':
## 
##     order_by, recode, rename
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(labelled)
## Warning: package 'labelled' was built under R version 4.5.3
library(writexl)
## Warning: package 'writexl' was built under R version 4.5.3
# ==============================================================================
# BƯỚC 1: MÃ HÓA CÁC BIẾN ĐO LƯỜNG CHỈNH NHA (MUTATE)
# ==============================================================================
df <- df %>%
  mutate(
    # --- CÁC BIẾN ĐO LƯỜNG ĐƠN LẺ ---
    across(any_of("abQ8_Overjet"), ~ factor(., levels = c(0, 1, 2, 3, 4), labels = c("Overjet <0", "Overjet =0", "Overjet 0.5 - 2.5", "Overjet 3.0 - 4.0", "Overjet >4.0"))),
    across(any_of("bQ9"), ~ factor(., levels = c(0, 1), labels = c("None", "Incompetence"))),
    across(any_of("cQ10"), ~ factor(., levels = c(0, 1, 2, 3, 4, 5), labels = c("None", "Front", "Posterior teeth on one side", "Posterior teeth on both sides", "Front & Posterior teeth on one side", "Front & Posterior teeth on both sides"))),
    across(any_of("cQ11_Score"), ~ factor(., levels = c(0, 1, 2, 3), labels = c("0", "<1 mm", "1 - 2 mm", "2 mm+"))),
    across(any_of("fQ15"), ~ factor(., levels = c(0, 1, 2), labels = c("None", "Impinging, no periodontal", "Impinging, periodontal lesion"))),
    across(any_of("hQ16"), ~ factor(., levels = c(0, 1, 2), labels = c("none", "1 tooth per", ">1 tooth per"))),
    across(any_of("sQ21"), ~ factor(., levels = c(0, 1), labels = c("None", "Have tooth...."))),
    
    # --- NHÓM KHOẢNG CÁCH (CROWDING, SPACING, OPEN BITE) ---
    across(any_of(c("dQ12_CrowScore", "dQ12_SpacScore", "eQ13_Score")), ~ factor(., levels = c(0, 1, 2, 3, 4), labels = c("=0", "<1 mm", "1 - 2 mm", ">2 - 4 mm", ">4 mm"))),
    
    # --- NHÓM YES/NO VÀ NONE/HAVE ---
    across(any_of(c("iQ17", "jQ18", "pQ20", "tQ22", "xQ23")), ~ factor(., levels = c(0, 1), labels = c("None", "Yes"))),
    across(any_of(c("Prema_Contact", "kQ19")), ~ factor(., levels = c(0, 1), labels = c("None", "Have"))),
    
    # --- NHÓM VỊ TRÍ RĂNG ĐẶC THÙ (LIỆT KÊ TRỰC TIẾP TỪNG RĂNG) ---
    across(any_of(c(
      # ABO1 (Negative overjet)
      "ABO1_13", "ABO1_12", "ABO1_11", "ABO1_21", "ABO1_22", "ABO1_23",
      
      # cQ10 (Lingual posterior crossbite positions)
      "cQ10_18", "cQ10_17", "cQ10_16", "cQ10_15", "cQ10_14", "cQ10_13", 
      "cQ10_23", "cQ10_24", "cQ10_25", "cQ10_26", "cQ10_27", "cQ10_28",
      
      # ABO2 (Open bite positions)
      "ABO2_17", "ABO2_16", "ABO2_15", "ABO2_14", "ABO2_13", "ABO2_12", "ABO2_11", 
      "ABO2_21", "ABO2_22", "ABO2_23", "ABO2_24", "ABO2_25", "ABO2_26", "ABO2_27",
      
      # ABO3 (Buccal posterior crossbite positions)
      "ABO3_17", "ABO3_16", "ABO3_15", "ABO3_14", "ABO3_13", "ABO3_12", "ABO3_11", 
      "ABO3_21", "ABO3_22", "ABO3_23", "ABO3_24", "ABO3_25", "ABO3_26", "ABO3_27"
    )), ~ factor(., levels = c(0, 1), labels = c("No", "Yes")))
  )

# ==============================================================================
# BƯỚC 2: GẮN NHÃN MÔ TẢ (LABELS) ĐẦY ĐỦ CHO TỪNG RĂNG VÀ CÁC BIẾN KHÁC
# ==============================================================================
ortho_labels <- list(
  abQ8 = "Overjet (mm)", abQ8_Score = "Overjet", abQ8_Overjet = "Overjet Group",
  ABO1 = "Negative overjet", bQ9 = "Lips incompetence", cQ10 = "Lingual posterior crossbite",
  cQ11 = "RCP - ICP (mm)", cQ11_Score = "RCP - ICP", dQ12_Crow = "Crowding (mm)",
  dQ12_CrowScore = "Crowding", dQ12_Spac = "Spacing (mm)", dQ12_SpacScore = "Spacing",
  eQ13 = "Open bite (mm)", eQ13_Score = "Open bite", fQ14 = "Overbite (mm)", fQ14_Score = "Overbite",
  fQ15 = "Impinging", hQ16 = "Missing teeth", hQ16_Numb = "number of missing teeth", hQ16_Po = "Position",
  iQ17 = "Impeded eruption (except 3rd molars)", jQ18 = "Buccal posterior crossbite",
  Prema_Contact = "Premature contact", PreCo_Po = "Position", kQ19 = "Dysfunction",
  pQ20 = "Cleft lip & palate", sQ21 = "Deciduous teeth", sQ21_Numb = "number of deciduous teeth",
  sQ21_Po = "Position", tQ22 = "Partially erupted", tQ22_Numb = "Number", tQ22_Po = "Position",
  xQ23 = "Supplemental tooth", xQ23_Numb = "Number", xQ23_Po = "Position", Q33 = "Aesthetic assessment",
  
  # === NHÃN CHO CÁC VỊ TRÍ RĂNG CỤ THỂ (ABO1, cQ10, ABO2, ABO3) ===
  
  # ABO1
  ABO1_13 = "Negative overjet - Tooth 13", ABO1_12 = "Negative overjet - Tooth 12", ABO1_11 = "Negative overjet - Tooth 11", 
  ABO1_21 = "Negative overjet - Tooth 21", ABO1_22 = "Negative overjet - Tooth 22", ABO1_23 = "Negative overjet - Tooth 23",
  
  # cQ10
  cQ10_18 = "Lingual posterior crossbite - Tooth 18", cQ10_17 = "Lingual posterior crossbite - Tooth 17", cQ10_16 = "Lingual posterior crossbite - Tooth 16",
  cQ10_15 = "Lingual posterior crossbite - Tooth 15", cQ10_14 = "Lingual posterior crossbite - Tooth 14", cQ10_13 = "Lingual posterior crossbite - Tooth 13",
  cQ10_23 = "Lingual posterior crossbite - Tooth 23", cQ10_24 = "Lingual posterior crossbite - Tooth 24", cQ10_25 = "Lingual posterior crossbite - Tooth 25",
  cQ10_26 = "Lingual posterior crossbite - Tooth 26", cQ10_27 = "Lingual posterior crossbite - Tooth 27", cQ10_28 = "Lingual posterior crossbite - Tooth 28",
  
  # ABO2
  ABO2_17 = "Open bite position - Tooth 17", ABO2_16 = "Open bite position - Tooth 16", ABO2_15 = "Open bite position - Tooth 15",
  ABO2_14 = "Open bite position - Tooth 14", ABO2_13 = "Open bite position - Tooth 13", ABO2_12 = "Open bite position - Tooth 12",
  ABO2_11 = "Open bite position - Tooth 11", ABO2_21 = "Open bite position - Tooth 21", ABO2_22 = "Open bite position - Tooth 22",
  ABO2_23 = "Open bite position - Tooth 23", ABO2_24 = "Open bite position - Tooth 24", ABO2_25 = "Open bite position - Tooth 25",
  ABO2_26 = "Open bite position - Tooth 26", ABO2_27 = "Open bite position - Tooth 27",
  
  # ABO3
  ABO3_17 = "Buccal posterior crossbite - Tooth 17", ABO3_16 = "Buccal posterior crossbite - Tooth 16", ABO3_15 = "Buccal posterior crossbite - Tooth 15",
  ABO3_14 = "Buccal posterior crossbite - Tooth 14", ABO3_13 = "Buccal posterior crossbite - Tooth 13", ABO3_12 = "Buccal posterior crossbite - Tooth 12",
  ABO3_11 = "Buccal posterior crossbite - Tooth 11", ABO3_21 = "Buccal posterior crossbite - Tooth 21", ABO3_22 = "Buccal posterior crossbite - Tooth 22",
  ABO3_23 = "Buccal posterior crossbite - Tooth 23", ABO3_24 = "Buccal posterior crossbite - Tooth 24", ABO3_25 = "Buccal posterior crossbite - Tooth 25",
  ABO3_26 = "Buccal posterior crossbite - Tooth 26", ABO3_27 = "Buccal posterior crossbite - Tooth 27"
)

# Cập nhật nhãn an toàn (Chỉ lấy những biến CÓ trong file df)
var_label(df) <- ortho_labels[names(ortho_labels) %in% names(df)]
# Tạo một bảng copy tạm thời để đổi tên tiêu đề
df_export <- df %>%
  # Lệnh này biến toàn bộ các "Nhãn dài" thành tên cột thực sự
  setNames(var_label(., unlist = TRUE))

# Sau đó xuất cái bảng tạm này ra Excel
write_xlsx(df_export, "D:\\TAM DAN - NON ORTHO (NEW)\\6.1\\06.1 Non ortho_IOTN.xlsx")