Các lệnh về data frame

Câu 1

Bạn có dataset về thông tin sinh viên của 1 trường Đại học như sau: students.csv. Hãy kiểm tra xem mã ID (cột stud.id) của sinh viên có bạn nào bị trùng nhau không? Nếu có hãy liệt kê các mã ID bị trùng.

df <- read.csv("dataset/students.csv")
dim(df)
[1] 8239   17
head(df)
  no stud.id                name gender age height weight   religion nc.score semester                  major                      minor score1 score2 online.tutorial graduated salary
1  1  833917 Gonzales, Christina Female  19    160   64.8     Muslim     1.91      1st      Political Science            Social Sciences     NA     NA               0         0     NA
2  2  898539      Lozano, T'Hani Female  19    172   73.0      Other     1.56      2nd        Social Sciences Mathematics and Statistics     NA     NA               0         0     NA
3  3  379678      Williams, Hanh Female  22    168   70.6 Protestant     1.24      3rd        Social Sciences Mathematics and Statistics     45     46               0         0     NA
4  4  807564         Nem, Denzel   Male  19    183   79.7      Other     1.37      2nd Environmental Sciences Mathematics and Statistics     NA     NA               0         0     NA
5  5  383291     Powell, Heather Female  21    175   71.4   Catholic     1.46      1st Environmental Sciences Mathematics and Statistics     NA     NA               0         0     NA
6  6  256074      Perez, Jadrian   Male  19    189   85.8   Catholic     1.34      2nd      Political Science Mathematics and Statistics     NA     NA               0         0     NA

Sử dụng lệnh unique() để trả về số lượng mã ID xuất hiện ít nhất một lần. Sau đó dùng lệnh length() để đếm xem có bao nhiêu mã ID unique, nếu tổng số mã ID unique bằng với số hàng của data frame có nghĩa là không có mã ID nào bị trùng, 100% là unique. Ngược lại, nếu tổng số mã ID unique nhỏ hơn số hàng của data frame có nghĩa là có vài mã ID bị trùng. Ta sẽ cần lọc ra.

df <- read.csv("dataset/students.csv")

length(unique(df$stud.id))
[1] 8237
dim(df)
[1] 8239   17

Ta thấy tổng số hàng của data frame này là 8239 lớn hơn tổng số mã ID unique là 8237, như vậy có 2 mã ID bị trùng.

table(df$stud.id) -> check_id

sort(check_id, decreasing = T) -> check_id

head(check_id)

188703 962284 110250 110348 110415 110615 
     2      2      1      1      1      1 
check_id[check_id > 1] -> id_dup
df_dup <- df[ which(df$stud.id %in% c(names(id_dup))) , ]

df_dup
     no stud.id            name gender age height weight religion nc.score semester                  major                      minor score1 score2 online.tutorial graduated   salary
34   34  188703  Torres, Andrew   Male  18    173   75.7    Other     1.19      4th Environmental Sciences Mathematics and Statistics     74     74               0         0       NA
63   63  188703   Cebrun, Linda Female  21    157   62.9 Catholic     2.59     >6th Environmental Sciences            Social Sciences     64     75               0         1 32519.22
73   73  962284 Iverson, Sierra Female  20    154   63.0    Other     2.30      1st                Biology     Environmental Sciences     NA     NA               0         0       NA
109 109  962284  John III, Ryan   Male  18    183   81.4 Catholic     1.84      2nd Environmental Sciences          Political Science     NA     NA               0         0       NA

Câu 2

Tiếp theo câu 1, bạn hãy tính trung bình độ tuổi age của sinh viên theo ngành học major và theo giới tính gender, thể hiện rõ tổng số lượng sinh viên trong mỗi nhóm. Kết quả sẽ tương tự như bảng sau:

                        major gender age_mean number_student
1                     Biology Female 22.56621            959
2                     Biology   Male 22.51411            638
3       Economics and Finance Female 22.44252            461
4       Economics and Finance   Male 22.52260            863
5      Environmental Sciences Female 22.84564            745
6      Environmental Sciences   Male 22.59478            881
7  Mathematics and Statistics Female 22.60870            276
8  Mathematics and Statistics   Male 22.30875            949
9           Political Science Female 22.51840            978
10          Political Science   Male 22.74843            477
11            Social Sciences Female 22.63965            691
12            Social Sciences   Male 22.04673            321
library(dplyr)

df |> dplyr:::group_by(major, gender) |> 
  dplyr:::summarise(age_mean = mean(age),
                    number_student = n()) |> as.data.frame() -> kq

kq
                        major gender age_mean number_student
1                     Biology Female 22.56621            959
2                     Biology   Male 22.51411            638
3       Economics and Finance Female 22.44252            461
4       Economics and Finance   Male 22.52260            863
5      Environmental Sciences Female 22.84564            745
6      Environmental Sciences   Male 22.59478            881
7  Mathematics and Statistics Female 22.60870            276
8  Mathematics and Statistics   Male 22.30875            949
9           Political Science Female 22.51840            978
10          Political Science   Male 22.74843            477
11            Social Sciences Female 22.63965            691
12            Social Sciences   Male 22.04673            321
sum(kq$number_student)
[1] 8239

Câu 3

Tương tự câu 2, bạn hãy tính trung bình mức lương salary của sinh viên theo ngành học major và theo giới tính gender, bởi vì cột salary có giá trị NA vì có một số sinh viên không có thông tin về lương, do đó cần thể hiện rõ tổng số lượng sinh viên đã dùng để tính toán trung bình trong mỗi nhóm (hay nói cách khác số lượng sinh viên có thông tin về mức lương trong mỗi nhóm). Kết quả sẽ tương tự như bảng sau:

                        major gender salary_mean number_student_salary number_student_NA number_student
1                     Biology Female    42266.67                   152               807            959
2                     Biology   Male    52789.89                   178               460            638
3       Economics and Finance Female    42452.84                    73               388            461
4       Economics and Finance   Male    52077.99                   222               641            863
5      Environmental Sciences Female    32487.97                   114               631            745
6      Environmental Sciences   Male    40201.49                   231               650            881
7  Mathematics and Statistics Female    40135.93                    48               228            276
8  Mathematics and Statistics   Male    50105.02                   240               709            949
9           Political Science Female    33417.02                   164               814            978
10          Political Science   Male    40291.19                   125               352            477
11            Social Sciences Female    29612.26                   123               568            691
12            Social Sciences   Male    35647.68                    83               238            321
library(dplyr)

df |> dplyr:::group_by(major, gender) |> 
  dplyr:::summarise(salary_mean = mean(salary, na.rm = T),
                    number_student_salary = length(which(!is.na(salary))),
                    number_student_NA = length(which(is.na(salary))),
                    number_student = n()
                    ) |> as.data.frame()
                        major gender salary_mean number_student_salary number_student_NA number_student
1                     Biology Female    42266.67                   152               807            959
2                     Biology   Male    52789.89                   178               460            638
3       Economics and Finance Female    42452.84                    73               388            461
4       Economics and Finance   Male    52077.99                   222               641            863
5      Environmental Sciences Female    32487.97                   114               631            745
6      Environmental Sciences   Male    40201.49                   231               650            881
7  Mathematics and Statistics Female    40135.93                    48               228            276
8  Mathematics and Statistics   Male    50105.02                   240               709            949
9           Political Science Female    33417.02                   164               814            978
10          Political Science   Male    40291.19                   125               352            477
11            Social Sciences Female    29612.26                   123               568            691
12            Social Sciences   Male    35647.68                    83               238            321