Việc 3: Đọc dữ liệu

ob = read.csv("/Users/Admin/Desktop/Lớp PTSL 8.9.2024/obesity_data.csv")

lệnh này để kết nối với file trong trường hợp “cannot open connection”

f <- file.choose()

sau đó copy đúng đường dẫn của giá trị f dán vào lệnh read.csv là được.

Việc 4

head(ob)
tail(ob)
summary(ob)
##        id            gender              height          weight     
##  Min.   :   1.0   Length:1217        Min.   :136.0   Min.   :34.00  
##  1st Qu.: 309.0   Class :character   1st Qu.:151.0   1st Qu.:49.00  
##  Median : 615.0   Mode  :character   Median :155.0   Median :54.00  
##  Mean   : 614.5                      Mean   :156.7   Mean   :55.14  
##  3rd Qu.: 921.0                      3rd Qu.:162.0   3rd Qu.:61.00  
##  Max.   :1227.0                      Max.   :185.0   Max.   :95.00  
##       bmi            age            WBBMC          wbbmd            fat       
##  Min.   :14.5   Min.   :13.00   Min.   : 695   Min.   :0.650   Min.   : 4277  
##  1st Qu.:20.2   1st Qu.:35.00   1st Qu.:1481   1st Qu.:0.930   1st Qu.:13768  
##  Median :22.2   Median :48.00   Median :1707   Median :1.010   Median :16955  
##  Mean   :22.4   Mean   :47.15   Mean   :1725   Mean   :1.009   Mean   :17288  
##  3rd Qu.:24.3   3rd Qu.:58.00   3rd Qu.:1945   3rd Qu.:1.090   3rd Qu.:20325  
##  Max.   :37.1   Max.   :88.00   Max.   :3040   Max.   :1.350   Max.   :40825  
##       lean           pcfat       hypertension      diabetes     
##  Min.   :19136   Min.   : 9.2   Min.   :0.000   Min.   :0.0000  
##  1st Qu.:30325   1st Qu.:27.0   1st Qu.:0.000   1st Qu.:0.0000  
##  Median :33577   Median :32.4   Median :1.000   Median :0.0000  
##  Mean   :35463   Mean   :31.6   Mean   :0.507   Mean   :0.1109  
##  3rd Qu.:39761   3rd Qu.:36.8   3rd Qu.:1.000   3rd Qu.:0.0000  
##  Max.   :63059   Max.   :48.4   Max.   :1.000   Max.   :1.0000

Việc 5: Biên tập dữ liệu bằng package tidyverse

Mã hoá biến gender (F/M) thành biến sex với giá trị 0/1 (0= M; 1= F)

ob$sex[ob$gender == "F"] = 1
ob$sex[ob$gender == "M"] = 0

Mã hoá biến bmi thành biến obese với 4 nhóm như sau:

Nếu bmi < 18.5 thì obese = “Underweight” 
Nếu 18.5  bmi < 25.0 thì obese = “Normal”  
Nếu 25.0  bmi < 29.9 thì obese = “Overweight” 
Nếu bmi ≥ 30.0 thì obese = “Obese”  
ob$obese[ob$bmi < 18.5] = "Underweight"
ob$obese[ob$bmi >= 18.5 & ob$bmi < 25.0] = "Normal"
ob$obese[ob$bmi >= 25.0 & ob$bmi < 29.9] = "Overweight"
ob$obese[ob$bmi >= 30.0] = "Obese"

Tạo biến số mới lean.kg và fat.kg tính toán lượng cơ (lean) và mỡ (fat) bằng đơn vị kg

ob$lean.kg = ob$lean/1000
ob$fat.kg = ob$fat/1000

DO NGOC THE