Mô tả dữ liệu: Fertility là dữ liệu chéo từ Điều tra dân số Hoa Kỳ năm 1980 về phụ nữ đã kết hôn ở độ tuổi 21-35 có hai con trở lên. Bảng dữ liệu gồm 254,654 quan sát và 8 biến:
morekids: mẹ có nhiều hơn 2 con không (yes/no)
gender1: giới tính của đứa con đầu tien (male/female)
gender2: giới tính của đứa con thứ 2 (male/female)
age: tuổi của người mẹ (numberic)
afam: Người mẹ có là người Mỹ gốc Phi (yes/no)
hispanic: Người mẹ có là người gốc Tây Ban Nha(yes/no)
other: Người mẹ không phải là người Mỹ gốc Phi, người gốc Tây Ban Nha hay người da trắng (yes/no)
work: số tuần mà người mẹ làm việc trong 1 năm
## morekids gender1 gender2 age afam hispanic other work
## 1 no male female 27 no no no 0
## 2 no female male 30 no no no 30
## 3 no male female 27 no no no 0
## 4 no male female 35 yes no no 0
## 5 no female female 30 no no no 22
## 6 no male female 26 no no no 40
## 7 no female male 29 no no no 0
## 8 no male male 33 no no no 52
## 9 no female male 29 no no no 0
## 10 no male female 27 no no no 0
Tôi có thể xuất dataset Fertility sang file .csv bằng hàm write.csv() như sau:
write.csv(Fertility, file="fertilitydata.csv")
Sau đó, file .csv ấy được tôi đổi thành file Excel .xlsx như với thông tin như sau:
Đầu tiên, dataset được lưu dưới dạng file excel được tôi xuất như bên trên đã được tôi lưu vào dữ liệu trực tuyến Google Drive.
Sau khi tải file excel Fertility_data_nkan.xlsx về máy, bạn có thể dùng package readxl và dùng hàm read_excel để nhập đường dẫn file excel được tải về vào R như sau:
library("readxl")
y <- read_excel("/Users/84896/Downloads/data_Fertility.xlsx", sheet="Fertility")
print(head(y,10))
## # A tibble: 10 × 9
## ...1 morekids gender1 gender2 age afam hispanic other work
## <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> <chr> <dbl>
## 1 1 no male female 27 no no no 0
## 2 2 no female male 30 no no no 30
## 3 3 no male female 27 no no no 0
## 4 4 no male female 35 yes no no 0
## 5 5 no female female 30 no no no 22
## 6 6 no male female 26 no no no 40
## 7 7 no female male 29 no no no 0
## 8 8 no male male 33 no no no 52
## 9 9 no female male 29 no no no 0
## 10 10 no male female 27 no no no 0
NhieuHonHai <- y$morekids
GioiTinhCon1 <- y$gender1
GioiTinhCon2 <- y$gender2
TuoiCuaMe <- y$age
SoTuanLd <- y$work
MyGocPhi <- y$afam
MyGocTBN <- y$hispanic
xyz <- data.frame(NhieuHonHai,GioiTinhCon1,GioiTinhCon2,TuoiCuaMe,SoTuanLd,MyGocPhi,MyGocTBN)
head(xyz,5)
## NhieuHonHai GioiTinhCon1 GioiTinhCon2 TuoiCuaMe SoTuanLd MyGocPhi MyGocTBN
## 1 no male female 27 0 no no
## 2 no female male 30 30 no no
## 3 no male female 27 0 no no
## 4 no male female 35 0 yes no
## 5 no female female 30 22 no no
table(NhieuHonHai)
## NhieuHonHai
## no yes
## 157742 96912
print(table(GioiTinhCon1))
## GioiTinhCon1
## female male
## 123670 130984
print(table(GioiTinhCon2))
## GioiTinhCon2
## female male
## 124131 130523
BangTuoiCuaMe <- cut(TuoiCuaMe, breaks = c(20, 25, 30, 35), labels = c("20-25", "25-30", "30-35"), right = TRUE)
tabTuoi=table(BangTuoiCuaMe)
tabTuoi
## BangTuoiCuaMe
## 20-25 25-30 30-35
## 26118 89055 139481
BangSoTuanLd <- cut(SoTuanLd, breaks = c(0, 20, 40, 52), labels = c("0-20", "20-40", "40-52"), right = TRUE)
tabLd=table(BangSoTuanLd)
tabLd
## BangSoTuanLd
## 0-20 20-40 40-52
## 35239 32269 67005
barplot(table(NhieuHonHai), xlab = "", ylab = "tần số", main = "Biểu đồ thể hiện dữ liệu của biến Nhiều hơn Hai con", col = c("red", "green"))
=> Nhận xét:
barplot(tabTuoi, xlab = "", ylab = "Thu Nhập", main = "Biểu đồ thể hiện dữ liệu của biến Tuổi Của Mẹ", col = c("red", "green", "blue", "pink", "white"))
=> Nhận xét:
barplot(table(SoTuanLd), xlab = "", ylab = "số tuần", main = "Biểu đồ thể hiện dữ liệu của biến Số Tuần Lao Động của người mẹ ")