Bài tập ngày 1

Việc 1: Tải R, RStudio về máy tính và cài đặt hai nhu liệu này.

Việc 2: Cài đặt các gói lệnh (packages) cần thiết (nếu chưa cài đặt).

Việc 3: Đọc dữ liệu”Obesity data.csv” vào R và gọi dữ liệu là “ob” (dùng hàm read.csv)

ob = read.csv("D:\\DAU\\NH 2025 - 2026\\13. TAP HUAN NCKH\\THUC HANH\\Obesity data.csv")
dim(ob)
## [1] 1217   13
head(ob)
##   id gender height weight  bmi age WBBMC wbbmd   fat  lean pcfat hypertension
## 1  1      F    150     49 21.8  53  1312  0.88 17802 28600  37.3            0
## 2  2      M    165     52 19.1  65  1309  0.84  8381 40229  16.8            1
## 3  3      F    157     57 23.1  64  1230  0.84 19221 36057  34.0            1
## 4  4      F    156     53 21.8  56  1171  0.80 17472 33094  33.8            1
## 5  5      M    160     51 19.9  54  1681  0.98  7336 40621  14.8            0
## 6  6      F    153     47 20.1  52  1358  0.91 14904 30068  32.2            1
##   diabetes
## 1        1
## 2        0
## 3        0
## 4        0
## 5        0
## 6        0
tail(ob)
##        id gender height weight  bmi age WBBMC wbbmd   fat  lean pcfat
## 1212 1222      F    153     50 21.4  59  1309  0.87 18328 29147  37.6
## 1213 1223      F    150     44 19.6  44  1474  0.95 12906 28534  30.1
## 1214 1224      F    148     51 23.3  58  1522  0.97 14938 33931  29.6
## 1215 1225      F    149     50 22.5  57  1409  0.93 16777 30598  34.4
## 1216 1226      F    144     49 23.6  67  1266  0.90 20094 27272  41.3
## 1217 1227      F    141     45 22.6  58  1228  0.91 14567 28111  33.2
##      hypertension diabetes
## 1212            1        0
## 1213            0        1
## 1214            0        0
## 1215            1        0
## 1216            1        0
## 1217            0        0

Việc 4: Thông tin về dữ liệu ob này

4.1 Có bao nhiêu biến số (variable) và quan sát (observation)

nrow(ob)
## [1] 1217
ncol(ob)
## [1] 13

4.2 Liệt kê 6 quan sát đầu tiên của dữ liệu.

head(ob)
##   id gender height weight  bmi age WBBMC wbbmd   fat  lean pcfat hypertension
## 1  1      F    150     49 21.8  53  1312  0.88 17802 28600  37.3            0
## 2  2      M    165     52 19.1  65  1309  0.84  8381 40229  16.8            1
## 3  3      F    157     57 23.1  64  1230  0.84 19221 36057  34.0            1
## 4  4      F    156     53 21.8  56  1171  0.80 17472 33094  33.8            1
## 5  5      M    160     51 19.9  54  1681  0.98  7336 40621  14.8            0
## 6  6      F    153     47 20.1  52  1358  0.91 14904 30068  32.2            1
##   diabetes
## 1        1
## 2        0
## 3        0
## 4        0
## 5        0
## 6        0

4.3 Liệt kê 6 quan sát cuối cùng của dữ liệu

tail(ob)
##        id gender height weight  bmi age WBBMC wbbmd   fat  lean pcfat
## 1212 1222      F    153     50 21.4  59  1309  0.87 18328 29147  37.6
## 1213 1223      F    150     44 19.6  44  1474  0.95 12906 28534  30.1
## 1214 1224      F    148     51 23.3  58  1522  0.97 14938 33931  29.6
## 1215 1225      F    149     50 22.5  57  1409  0.93 16777 30598  34.4
## 1216 1226      F    144     49 23.6  67  1266  0.90 20094 27272  41.3
## 1217 1227      F    141     45 22.6  58  1228  0.91 14567 28111  33.2
##      hypertension diabetes
## 1212            1        0
## 1213            0        1
## 1214            0        0
## 1215            1        0
## 1216            1        0
## 1217            0        0

4.4 Tóm tắt dữ liệu bằng hàm summary

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

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

ob$sex = ifelse(ob$gender == "F", 1, 0)
head(ob)
##   id gender height weight  bmi age WBBMC wbbmd   fat  lean pcfat hypertension
## 1  1      F    150     49 21.8  53  1312  0.88 17802 28600  37.3            0
## 2  2      M    165     52 19.1  65  1309  0.84  8381 40229  16.8            1
## 3  3      F    157     57 23.1  64  1230  0.84 19221 36057  34.0            1
## 4  4      F    156     53 21.8  56  1171  0.80 17472 33094  33.8            1
## 5  5      M    160     51 19.9  54  1681  0.98  7336 40621  14.8            0
## 6  6      F    153     47 20.1  52  1358  0.91 14904 30068  32.2            1
##   diabetes sex
## 1        1   1
## 2        0   0
## 3        0   1
## 4        0   1
## 5        0   0
## 6        0   1

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

ob$obse[ob$bmi<18.5]="Thiếu cân"
ob$obse[ob$bmi >= 18.5 & ob$bmi < 25] = "Bình thường"
ob$obse[ob$bmi>=25 & ob$bmi<30]= "Thừa cân"
ob$obse[ob$bmi>=30]="Béo phì"
head(ob)
##   id gender height weight  bmi age WBBMC wbbmd   fat  lean pcfat hypertension
## 1  1      F    150     49 21.8  53  1312  0.88 17802 28600  37.3            0
## 2  2      M    165     52 19.1  65  1309  0.84  8381 40229  16.8            1
## 3  3      F    157     57 23.1  64  1230  0.84 19221 36057  34.0            1
## 4  4      F    156     53 21.8  56  1171  0.80 17472 33094  33.8            1
## 5  5      M    160     51 19.9  54  1681  0.98  7336 40621  14.8            0
## 6  6      F    153     47 20.1  52  1358  0.91 14904 30068  32.2            1
##   diabetes sex        obse
## 1        1   1 Bình thường
## 2        0   0 Bình thường
## 3        0   1 Bình thường
## 4        0   1 Bình thường
## 5        0   0 Bình thường
## 6        0   1 Bình thường

5.3 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
head(ob)
##   id gender height weight  bmi age WBBMC wbbmd   fat  lean pcfat hypertension
## 1  1      F    150     49 21.8  53  1312  0.88 17802 28600  37.3            0
## 2  2      M    165     52 19.1  65  1309  0.84  8381 40229  16.8            1
## 3  3      F    157     57 23.1  64  1230  0.84 19221 36057  34.0            1
## 4  4      F    156     53 21.8  56  1171  0.80 17472 33094  33.8            1
## 5  5      M    160     51 19.9  54  1681  0.98  7336 40621  14.8            0
## 6  6      F    153     47 20.1  52  1358  0.91 14904 30068  32.2            1
##   diabetes sex        obse lean.kg fat.kg
## 1        1   1 Bình thường  28.600 17.802
## 2        0   0 Bình thường  40.229  8.381
## 3        0   1 Bình thường  36.057 19.221
## 4        0   1 Bình thường  33.094 17.472
## 5        0   0 Bình thường  40.621  7.336
## 6        0   1 Bình thường  30.068 14.904

5.4 Tạo 1 tập dữ liệu men.overweight gồm nam giới quá cân và béo phì

men.overweight = subset(ob,gender=="M" & bmi>=25)
head(men.overweight)
##    id gender height weight  bmi age WBBMC wbbmd   fat  lean pcfat hypertension
## 16 16      M    150     70 31.1  49  2084  1.00 16540 49512  24.3            1
## 18 18      M    158     65 26.0  49  2557  1.29 11716 45567  19.6            1
## 19 19      M    162     72 27.4  66  1535  0.86 25416 44577  35.5            1
## 26 26      M    172     78 26.4  52  1991  0.95 19772 55200  25.7            0
## 40 40      M    171     75 25.6  51  2192  1.13 18188 51801  25.2            1
## 69 69      M    168     80 28.3  53  2043  1.06 17907 54594  24.0            0
##    diabetes sex     obse lean.kg fat.kg
## 16        0   0  Béo phì  49.512 16.540
## 18        0   0 Thừa cân  45.567 11.716
## 19        0   0 Thừa cân  44.577 25.416
## 26        0   0 Thừa cân  55.200 19.772
## 40        0   0 Thừa cân  51.801 18.188
## 69        0   0 Thừa cân  54.594 17.907

5.5 Tạo 1 tập dữ liệu Demo chỉ bao gồm 6 biến số là id, age, gender, height, weight và pcfat.

Demo = ob[, c("id", "age", "gender", "height", "weight", "pcfat")]
head(Demo)
##   id age gender height weight pcfat
## 1  1  53      F    150     49  37.3
## 2  2  65      M    165     52  16.8
## 3  3  64      F    157     57  34.0
## 4  4  56      F    156     53  33.8
## 5  5  54      M    160     51  14.8
## 6  6  52      F    153     47  32.2

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.