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\\10. Non ortho_EXAMINATION OF TEETH AND PERIDONTICAL CONDITION\\10.3 PLAQUE INDEX AND CALCULUS INDEX SIMPLIFIED\\10.3 PLAQUE INDEX AND CALCULUS INDEX SIMPLIFIED.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(labelled)
## Warning: package 'labelled' was built under R version 4.5.3
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(writexl)
## Warning: package 'writexl' was built under R version 4.5.3
# 1. MÃ HÓA CÁC BIẾN PI VÀ CIS GHI ĐÈ LÊN df
df <- df %>%
  mutate(
    # --- Mã hóa nhóm mảng bám (Plaque Index - PI) ---
    across(
      starts_with("PI"),
      ~ factor(., levels = c(0, 1, 2, 3, 888, 999), 
               labels = c("No plaque", 
                          "Plaque ≤ 1/3 of tooth surface or sticky stains without plaque", 
                          "Plaque covering between > 1/3 and ≤ 2/3 of tooth surface", 
                          "Plaque covering ≥ 2/3 of tooth surface", 
                          "Not recorded", 
                          "Missing tooth"))
    ),
    
    # --- Mã hóa nhóm vôi răng (Calculus Index Simplified - CIS) ---
    # Dùng starts_with("CI") để bao hàm cả biến CIS_... và biến CI_17(N) trong ảnh
    across(
      starts_with("CI"),
      ~ factor(., levels = c(0, 1, 2, 3, 888, 999), 
               labels = c("No calculus present", 
                          "Supragingival calculus covering not more than third of the exposed tooth surface", 
                          "Supragingival calculus covering more than > 1/3 and ≤ 2/3 of the exposed tooth surface", 
                          "Supragingival calculus covering ≥ 2/3 of the exposed tooth surface", 
                          "Not recorded", 
                          "Missing tooth"))
    )
  )

# 2. GẮN NHÃN MÔ TẢ (LABELS) TỰ ĐỘNG CHO MẶT TRONG/NGOÀI
# Bước 2.1: Lấy danh sách tên các biến bắt đầu bằng PI hoặc CI
pi_ci_vars <- df %>% select(starts_with("PI"), starts_with("CI")) %>% names()

# Bước 2.2: Tự động thay thế văn bản để tạo nhãn mô tả đẹp mắt
# Lệnh này sẽ tự động tìm ký hiệu (N), (T) kể cả khi có khoảng trắng hay viết liền
nhan_pi_ci <- pi_ci_vars %>%
  gsub("^PI_", "Plaque Index Tooth ", .) %>%
  gsub("^CIS_", "Calculus Index Simplified Tooth ", .) %>%
  gsub("^CI_", "Calculus Index Simplified Tooth ", .) %>% # Xử lý riêng cho CI_17
  gsub("\\s?\\(N\\)", " - Mặt ngoài (Facial/Buccal)", .) %>%
  gsub("\\s?\\(T\\)", " - Mặt trong (Lingual/Palatal)", .)

# Bước 2.3: Gán tên danh sách và đưa nhãn vào bảng df
names(nhan_pi_ci) <- pi_ci_vars
var_label(df) <- as.list(nhan_pi_ci)
# 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)\\10.3\\10.3 PLAQUE INDEX AND CALCULUS INDEX SIMPLIFIED.xlsx")