Bài tập ngày 1
Việc 1
Việc 2
ob=read.csv("/Users/tramphan/Documents/Tập huấn Phân tích dữ liệu 10.2025/Du lieu ngay 21.10.2025/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
summary(ob$height)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 136.0 151.0 155.0 156.7 162.0 185.0
Biên tập dữ liệu
ob$sex[ob$gender=="M"]=0
ob$sex[ob$gender=="F"]=1
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
ob$obese[ob$bmi<18.5]="Thiếu cân"
ob$obese[ob$bmi>=18.5 & ob$bmi<25]="Bình thường"
ob$obese[ob$bmi>=25 & ob$bmi<30]="Thừa cân"
ob$obese[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 obese
## 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
Tạo tập dữ liệu mới
men.overweight=subset(ob, gender=="M" & bmi>=25)
dim(men.overweight)
## [1] 85 15
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 obese
## 16 0 0 Béo phì
## 18 0 0 Thừa cân
## 19 0 0 Thừa cân
## 26 0 0 Thừa cân
## 40 0 0 Thừa cân
## 69 0 0 Thừa cân
dim(men.overweight)
## [1] 85 15
demo=subset(ob, select = c( age, gender, height, weight))
dim(demo)
## [1] 1217 4