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\\12 Non Ortho - SLEEP DISORDER SCREENING\\EDIT 12. Non Ortho_SLEEP DISORDER SCREENING.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 THEO TỪNG NHÓM THANG ĐO VÀ GHI ĐÈ LÊN df
df <- df %>%
  mutate(
    # --- 1. Nhóm Giới tính (Gender) ---
    Gender = factor(Gender, levels = c(0, 1), labels = c("Male", "Female")),
    
    # --- 2. Nhóm Yes/No cơ bản (TS1, TS2, TS3, TS4, PB7, PB8, BE8) ---
    across(
      c(TS1.1, TS1.2, TS1.3, TS1.4, TS1.5, TS1.6, TS1.7, TS1.8, 
        TS2.1, TS2.2, TS2.3, TS2.4, TS2.5, TS2.6, TS2.7, TS2.8, 
        TS3, TS4, 
        PB7A, PB7B, PB7C, PB7D, PB7E, PB7F, 
        PB8A, PB8B, PB8C, PB8D, 
        BE8),
      ~ factor(., levels = c(0, 1), labels = c("No", "Yes"))
    ),
    
    # --- 3. Nhóm Yes/No/Don't know (PSQ toàn bộ, BE1, BE4, BE10) ---
    across(
      c(PSQ1, PSQ2, PSQ3, PSQ4, PSQ5, PSQ6A, PSQ6B, PSQ7, PSQ8, PSQ9, PSQ10, 
        PSQ11, PSQ12, PSQ13, PSQ14, PSQ15, PSQ16, PSQ17, PSQ18, PSQ19, PSQ20, PSQ21, PSQ22,
        BE1, BE4, BE10),
      ~ factor(., levels = c(0, 1, 2), labels = c("No", "Yes", "Don't know"))
    ),
    
    # --- 4. Nhóm Tần suất giấc ngủ PB9 & PB10-PB12 ---
    across(
      c(PB9A, PB9B, PB9C, PB9D, PB9E, PB9F, PB9G, PB9H, PB9I, PB10, PB11, PB12),
      ~ factor(., levels = c(0, 1, 2, 3), 
               labels = c("Not during the past month", 
                          "Less than one a week", 
                          "One or twice a week", 
                          "Three or more times a week"))
    ),
    
    # --- 5. Nhóm Thang đo Epworth (ESS1 - ESS8) ---
    across(
      c(ESS1, ESS2, ESS3, ESS4, ESS5, ESS6, ESS7, ESS8),
      ~ factor(., levels = c(0, 1, 2, 3), 
               labels = c("Would never doze", 
                          "Slight chance of dozing", 
                          "Moderate chance of dozing", 
                          "High chance of dozing")) # Đã sửa Hight thành High
    ),
    
    # --- 6. Nhóm Tần suất ngáy Berlin (BE3, BE6, BE7, BE9) ---
    across(
      c(BE3, BE6, BE7, BE9),
      ~ factor(., levels = c(0, 1, 2, 3, 4), 
               labels = c("Nearly every day", 
                          "3-4 times a week", 
                          "1-2 times a week", 
                          "1-2 times a month", 
                          "Never or nearly never"))
    ),
    
    # --- 7. Các biến có thang đo đặc thù riêng (PB13, PB14, PB15, BE2, BE5, BE5A, B, C) ---
    PB13 = factor(PB13, levels = c(0, 1, 2, 3), 
                  labels = c("No problem at all", "Only a very slight problem", "Somewhat of a problem", "A very big problem")),
    
    PB14 = factor(PB14, levels = c(0, 1, 2, 3), 
                  labels = c("Very good", "Fairly good", "Fairly bad", "Very bad")),
    
    PB15 = factor(PB15, levels = c(0, 1, 2, 3), 
                  labels = c("Never", "Occasionally", "Often", "Always")),
    
    BE2 = factor(BE2, levels = c(0, 1, 2, 3), 
                 labels = c("Slightly louder than breathing", "As loud as talking", "Louder than talking", "Very loud - can be heard in adjacent rooms")),
    
    BE5 = factor(BE5, levels = c(0, 1, 2, 3, 4), 
                 labels = c("Almost every day", "3-4 times per week", "1-2 times per week", "1-2 times per month", "Rarely or never")),
    
    BE5A = factor(BE5A, levels = c(0, 1, 2, 3), 
                  labels = c("Every night", "More than 50% of nights", "Less than 50% of nights", "Rarely or never")),
    
    BE5B = factor(BE5B, levels = c(0, 1, 2, 3), 
                  labels = c("All night", "More than 50% of night", "Less than 50% of night", "Very short or none")),
    
    BE5C = factor(BE5C, levels = c(0, 1, 2, 3), 
                  labels = c("Audible from a lower floor", "Audible from the next room", "Audible within the same room", "Barely audible"))
  )

# 2. GẮN NHÃN MÔ TẢ (LABELS) CHUẨN XÁC THEO TỪNG CÂU HỎI
df <- df %>%
  set_variable_labels(
    Gender = "Gender",
    
    # Nhóm Tiền sử (TS)
    TS1.1 = "History of premature cut surgery",
    TS1.2 = "History of tongue cut surgery",
    TS1.3 = "History of tonsillectomy and/or adenoidectomy",
    TS1.4 = "Treatment with experts before this survey",
    TS1.5 = "Orthodontic history",
    TS1.6 = "Maxillofacial trauma or surgery",
    TS1.7 = "History of cleft lip and/or palate",
    TS1.8 = "Treatment for dysfunction",
    TS2.1 = "Anxiety/depression", # Đã sửa lỗi đánh máy
    TS2.2 = "Temporomandibular joint disorder",
    TS2.3 = "Chatter",
    TS2.4 = "Poor sleep quality", # Đã sửa lỗi đánh máy
    TS2.5 = "Exhausted",
    TS2.6 = "Medical conditions being treated if any",
    TS2.7 = "Allergy",
    TS2.8 = "None of the above",
    TS3 = "General health self-assessment is normal",
    TS4 = "Are you and your family willing to cooperate",
    
    # Nhóm PSQ
    PSQ1 = "Does he/she snore more than half the night?",
    PSQ2 = "Does he/she often snore?",
    PSQ3 = "Does he/she snore noisily?",
    PSQ4 = "Does he/she breathe loudly or heavily?",
    PSQ5 = "Does he/she have trouble breathing, or effort to breathe?",
    PSQ6A = "Have you ever seen your child stop breathing?",
    PSQ6B = "Have you ever observed him/her waking up with gasping or snorting",
    PSQ7 = "Does he/she breathe out of his/her mouth during the day?",
    PSQ8 = "Does he/she have a dry mouth when awake in the morning?",
    PSQ9 = "Does he/she wet the bed?",
    PSQ10 = "Does he/she awake in the morning without being refreshed?",
    PSQ11 = "Does he/she have trouble with sleepiness in the daytime?",
    PSQ12 = "Have any teachers/instructors commented that you seem sleepy during the day?",
    PSQ13 = "Is he/she difficult to awaken in the morning?",
    PSQ14 = "Does he/she awake in the morning with headaches?",
    PSQ15 = "Has he/she ceased growing normally since birth?",
    PSQ16 = "Is he/she overweight?",
    PSQ17 = "He/she doesn't appear to listen when you talk to him/her directly",
    PSQ18 = "He/she has trouble organizing duties and activities",
    PSQ19 = "He/she is easily distracted by foreign stimulation",
    PSQ20 = "He/she violins with hands or feet, or twitching in seating",
    PSQ21 = "He/she is 'on the move' or often acts like he/she is 'powered by an engine'",
    PSQ22 = "He/she interrupts/disturbs others",
    
    # Nhóm PSQI (PB)
    PB1A = "Bedtime is usually",
    PB1B = "Weekend bedtime is",
    PB2A = "Weekdays, the number of minutes usually to fall asleep",
    PB2B = "Weekends, minutes are usually to fall asleep",
    PB3A = "On weekdays, what time did you usually wake up",
    PB3B = "On weekends, what time did you usually wake up",
    PB4A = "On a normal day, the number of hours of sleep per night is usually",
    PB4B = "On weekends, the number of hours of sleep per night is usually",
    PB5A = "Noon nap (Number of short time a day)",
    PB5B = "Afternoon nap (Number of short time a day)",
    PB6A = "Average nap time? Noon nap",
    PB6B = "Average nap time? Afternoon nap",
    PB7A = "Usual activities 30 minutes before bedtime? Watching TV",
    PB7B = "Usual activities 30 minutes before bedtime? Using phone",
    PB7C = "Usual activities 30 minutes before bedtime? Playing video games",
    PB7D = "Usual activities 30 minutes before bedtime? Reading",
    PB7E = "Usual activities 30 minutes before bedtime? Using computer",
    PB7F = "Usual activities 30 minutes before bedtime? Drinking coffee/tea",
    PB8A = "Common Sleeping Positions: Supine (lying on back)",
    PB8B = "Common Sleeping Positions: Mostly supine > side/stomach",
    PB8C = "Common Sleeping Positions: Side/stomach",
    PB8D = "Common Sleeping Positions: Mostly side/stomach > supine",
    PB9A = "Cannot get to sleep within 30 minutes",
    PB9B = "Wake up in the middle of the night or early morning",
    PB9C = "Have to get up to use the bathroom",
    PB9D = "Cannot breathe comfortably",
    PB9E = "Cough and snore loudly",
    PB9F = "Feel too cold",
    PB9G = "Feel too hot",
    PB9H = "Having bad dreams",
    PB9I = "Have pain",
    PB9J = "Other reasons",
    PB10 = "Has this problem often caused you to have trouble sleeping?",
    PB11 = "How often have you taken medicine to help you sleep?",
    PB12 = "Had trouble staying awake while driving, eating meals, or engaging in social activity?",
    PB13 = "How much of a problem has it been for you to keep up enough enthusiasm to get things done?",
    PB14 = "How would you rate your sleep quality overall?",
    PB15 = "Do you often wake up frequently or experience restless sleep, tossing and turning?",
    
    # Nhóm ESS
    ESS1 = "Sitting and reading",
    ESS2 = "Watching TV",
    ESS3 = "Sitting, inactive in a public place",
    ESS4 = "As a passenger in a car for an hour without a break",
    ESS5 = "Lying down to rest in the afternoon when circumstances permit",
    ESS6 = "Sitting and talking to someone",
    ESS7 = "Sitting quietly after a lunch without alcohol",
    ESS8 = "In a car, while stopped for a few minutes in the traffic",
    
    # Nhóm Berlin Questionnaire (BE)
    BE1 = "Do you snore?",
    BE2 = "How loud is your snoring?",
    BE3 = "How often do you snore?",
    BE4 = "Does your snoring bother others?",
    BE5 = "Has anyone noticed that you quit breathing during your sleep",
    BE5A = "Snoring Frequency",
    BE5B = "Duration of Snoring",
    BE5C = "Loudness of Snoring",
    BE6 = "How often do you feel tired or fatigued after you sleep?",
    BE7 = "During your waking time, do you feel tired, fatigued or not up to par?",
    BE8 = "Have you ever nodded off or fallen asleep while driving a vehicle",
    BE9 = "How often does this occur?",
    BE10 = "Do you have high blood pressure"
  )
# 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)\\12\\12. Non Ortho_SLEEP DISORDER SCREENING.xlsx")