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.csv2("D:\\TAM DAN NON-ORTHO\\06. Non ortho_OCCLUSION\\06.2 Non ortho_DAI & HDL SCALE\\06.2 Non ortho_DAI & HDL SCALE.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
# 1. MÃ HÓA VÀ GHI ĐÈ TRỰC TIẾP LÊN BIẾN GỐC
df <- df %>%
  mutate(
    # --- GENDER ---
    Gender = factor(Gender, levels = c(0, 1), labels = c("Male", "Female")),
    
    # --- NHÓM RĂNG CHEN CHÚC/KHE THƯA (ARCH) ---
    across(
      c(Q26_Arch, Q27_Arch),
      ~ factor(., levels = c(0, 1, 2, 3), 
               labels = c("None", "Have 1 maxillary arch", "Have 1 mandibular arch", "Two arches"))
    ),
    
    # --- TƯƠNG QUAN RĂNG NANH (Canine) ---
    across(
      c(abQ8_RCanine, abQ8_LCanine),
      ~ factor(., levels = c(1, 2, 3, 4, 999), 
               labels = c("Class I - END ON", "END ON to Class II/Class III", "Class II/Class III", "Beyond Class II/Class III", "No recorded"))
    ),
    
    # --- TƯƠNG QUAN RĂNG HÀM (Molar) ---
    # Tạm thời chỉ mã hóa RMolar vì cột LMolar trong file của bạn đang bị sai tên thành RMolar
    eQ13_RMolar = factor(eQ13_RMolar, levels = c(1, 2, 3, 999), 
                         labels = c("Cusp to fossa relationship 0", "Cusp to fossa - END ON", "END ON", "No recorded")),
    
    # --- PHÂN LOẠI ANGLE (Q31) ---
    across(
      c(Q31_RCanine, Q31_LCanine, Q31_RMolar, Q31_LMolar),
      ~ factor(., levels = c(0, 1, 2, 999), 
               labels = c("Normal", "1/2 cusp", "A cusp+", "No recorded"))
    ),
    
    # --- NHÓM NONE/HAVE ---
    across(
      c(Q28, Q36, Q37),
      ~ factor(., levels = c(0, 1), labels = c("None", "Have"))
    ),
    
    # --- CÁC BIẾN CÒN LẠI ---
    Q32 = factor(Q32, levels = c(0, 1), labels = c("Anterial", "Posterial")),
    
    Q35 = factor(Q35, levels = c(0), labels = c("None")) 
  )

# 2. GẮN NHÃN MÔ TẢ (LABELS) CHO TOÀN BỘ CÁC BIẾN
df <- df %>%
  set_variable_labels(
    Gender = "Gender",
    
    # --- CÁC BIẾN MỚI BỔ SUNG TỪ HÌNH ẢNH ---
    Q24 = "Number of missing visible teeth in the maxillary arch",
    Q24_Po = "Position",
    Q25 = "Number of missing visible teeth in the mandibular arch",
    Q25_Po = "Position",
    Q26 = "Crowding in the incisal segments of the arch (mm)",
    Q27 = "Spacing in the incisal segment of the arch (mm)",
    Q28_mm = "Midline diastema (mm)", 
    
    # --- CÁC BIẾN CŨ ĐÃ CÓ ---
    Q26_Arch = "Crowding in the incisal segments of the arch",
    Q27_Arch = "Spacing in the incisal segment of the arch",
    Q28 = "Midline diastema",
    Q29 = "Largest anterior irregularity on the maxillary",
    Q30 = "Largest anterior irregularity on the mandibular",
    abQ8_RCanine = "Canine Relation right",
    abQ8_LCanine = "Canine Relation left",
    
    eQ13_RMolar = "Molar relation right",
    eQ13_LMolar = "Molar Relation left", 
    
    Q31_RCanine = "Canine relation right (Angle classification)",
    Q31_LCanine = "Canine relation left (Angle classification)",
    Q31_RMolar = "Molar relation right (in bilateral occlusal relationship)",
    Q31_LMolar = "Molar relation left (in bilateral occlusal relationship)",
    Q32 = "Correlation of the maxillary and mandibular",
    
    Q35 = "Ectopic eruption >50%",
    Q36 = "Severe traumatic deviation",
    Q37 = "Cranio-facial anomaly"
  )
# 1. Cài đặt thư viện (Chỉ cần chạy 1 lần ở Console nếu máy chưa có)
# install.packages("writexl")

# 2. Gọi thư viện
library(writexl)
## Warning: package 'writexl' was built under R version 4.5.3
# 3. Xuất file Excel
# Lưu vào một đường dẫn cụ thể trên máy tính của bạn:
write_xlsx(df, "D:\\TAM DAN - NON ORTHO (NEW)\\6.2\\06.2 Non ortho_DAI & HDL SCALE.xlsx")
library(writexl)

# 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.2\\06.2 Non ortho_DAI & HDL SCALE.xlsx")