Question 1
Read file marks1.csv
dataset <- read.csv("marks1.csv")
dataset## X X.1 test asgn Prsnt Final q1 q2 q3 q4
## 1 60001 Ahmad 15 14 17 13 0.0 9 2 4.0
## 2 60003 Abu 26 13 18 22 3.0 5 8 6.0
## 3 60006 Samy 21 15 19 25 6.0 7 4 8.0
## 4 60008 Chong 25 10 17 14 2.0 3 4 5.0
## 5 60009 Paul 25 15 16 20 3.0 7 6 4.0
## 6 60011 John 18 15 19 22 4.0 7 4 7.0
## 7 60014 Devi 30 15 19 28 4.0 5 9 10.0
## 8 60015 Pillip 16 15 19 20 4.0 5 6 5.0
## 9 60023 Meilin 18 13 18 22 2.0 5 7 8.0
## 10 60025 Lily 30 14 18 24 5.5 6 5 7.5
## 11 60026 Jamil 12 10 12 12 1.0 5 1 6.0
Question 2
Check the data frame info using a few available functions
typeof(dataset)## [1] "list"
class(dataset)## [1] "data.frame"
summary(dataset)## X X.1 test asgn
## Min. :60001 Length:11 Min. :12.00 Min. :10.00
## 1st Qu.:60007 Class :character 1st Qu.:17.00 1st Qu.:13.00
## Median :60011 Mode :character Median :21.00 Median :14.00
## Mean :60013 Mean :21.45 Mean :13.55
## 3rd Qu.:60019 3rd Qu.:25.50 3rd Qu.:15.00
## Max. :60026 Max. :30.00 Max. :15.00
## Prsnt Final q1 q2
## Min. :12.00 Min. :12.00 Min. :0.000 Min. :3.000
## 1st Qu.:17.00 1st Qu.:17.00 1st Qu.:2.000 1st Qu.:5.000
## Median :18.00 Median :22.00 Median :3.000 Median :5.000
## Mean :17.45 Mean :20.18 Mean :3.136 Mean :5.818
## 3rd Qu.:19.00 3rd Qu.:23.00 3rd Qu.:4.000 3rd Qu.:7.000
## Max. :19.00 Max. :28.00 Max. :6.000 Max. :9.000
## q3 q4
## Min. :1.000 Min. : 4.000
## 1st Qu.:4.000 1st Qu.: 5.000
## Median :5.000 Median : 6.000
## Mean :5.091 Mean : 6.409
## 3rd Qu.:6.500 3rd Qu.: 7.750
## Max. :9.000 Max. :10.000
Question 3
Check the names of the variables in the data frame
names(dataset)## [1] "X" "X.1" "test" "asgn" "Prsnt" "Final" "q1" "q2" "q3"
## [10] "q4"
Question 4
Rename the first variable X to ID
names(dataset)[names(dataset) == "X"] <- "ID"
dataset## ID X.1 test asgn Prsnt Final q1 q2 q3 q4
## 1 60001 Ahmad 15 14 17 13 0.0 9 2 4.0
## 2 60003 Abu 26 13 18 22 3.0 5 8 6.0
## 3 60006 Samy 21 15 19 25 6.0 7 4 8.0
## 4 60008 Chong 25 10 17 14 2.0 3 4 5.0
## 5 60009 Paul 25 15 16 20 3.0 7 6 4.0
## 6 60011 John 18 15 19 22 4.0 7 4 7.0
## 7 60014 Devi 30 15 19 28 4.0 5 9 10.0
## 8 60015 Pillip 16 15 19 20 4.0 5 6 5.0
## 9 60023 Meilin 18 13 18 22 2.0 5 7 8.0
## 10 60025 Lily 30 14 18 24 5.5 6 5 7.5
## 11 60026 Jamil 12 10 12 12 1.0 5 1 6.0
Question 5
Rename the second variable X.1 to StuName
names(dataset)[names(dataset) == "X.1"] <- "StuName"
dataset## ID StuName test asgn Prsnt Final q1 q2 q3 q4
## 1 60001 Ahmad 15 14 17 13 0.0 9 2 4.0
## 2 60003 Abu 26 13 18 22 3.0 5 8 6.0
## 3 60006 Samy 21 15 19 25 6.0 7 4 8.0
## 4 60008 Chong 25 10 17 14 2.0 3 4 5.0
## 5 60009 Paul 25 15 16 20 3.0 7 6 4.0
## 6 60011 John 18 15 19 22 4.0 7 4 7.0
## 7 60014 Devi 30 15 19 28 4.0 5 9 10.0
## 8 60015 Pillip 16 15 19 20 4.0 5 6 5.0
## 9 60023 Meilin 18 13 18 22 2.0 5 7 8.0
## 10 60025 Lily 30 14 18 24 5.5 6 5 7.5
## 11 60026 Jamil 12 10 12 12 1.0 5 1 6.0
Question 6
Remove the first two column from the data frame
dataset_remove <- dataset
dataset_remove[, c(1,2)] <- NULL
dataset_remove## test asgn Prsnt Final q1 q2 q3 q4
## 1 15 14 17 13 0.0 9 2 4.0
## 2 26 13 18 22 3.0 5 8 6.0
## 3 21 15 19 25 6.0 7 4 8.0
## 4 25 10 17 14 2.0 3 4 5.0
## 5 25 15 16 20 3.0 7 6 4.0
## 6 18 15 19 22 4.0 7 4 7.0
## 7 30 15 19 28 4.0 5 9 10.0
## 8 16 15 19 20 4.0 5 6 5.0
## 9 18 13 18 22 2.0 5 7 8.0
## 10 30 14 18 24 5.5 6 5 7.5
## 11 12 10 12 12 1.0 5 1 6.0
Question 7
Use apply() function to sum all the marks in the data frame and put them in a new vector called Total and bind the vector to the data frame
dataset$total <- apply(dataset[, 3:10], 1, function(x) { sum(x) })
dataset## ID StuName test asgn Prsnt Final q1 q2 q3 q4 total
## 1 60001 Ahmad 15 14 17 13 0.0 9 2 4.0 74
## 2 60003 Abu 26 13 18 22 3.0 5 8 6.0 101
## 3 60006 Samy 21 15 19 25 6.0 7 4 8.0 105
## 4 60008 Chong 25 10 17 14 2.0 3 4 5.0 80
## 5 60009 Paul 25 15 16 20 3.0 7 6 4.0 96
## 6 60011 John 18 15 19 22 4.0 7 4 7.0 96
## 7 60014 Devi 30 15 19 28 4.0 5 9 10.0 120
## 8 60015 Pillip 16 15 19 20 4.0 5 6 5.0 90
## 9 60023 Meilin 18 13 18 22 2.0 5 7 8.0 93
## 10 60025 Lily 30 14 18 24 5.5 6 5 7.5 110
## 11 60026 Jamil 12 10 12 12 1.0 5 1 6.0 59
Question 8
Using a user defined function called function(), use the apply() function to add variable 1 to variable 3,and write to a new variable in the data frame called CW.function(x) {x[1]+x[2]+x[3]}
dataset$CW <- apply(dataset[, 3:5], 1, function(x) { x[1]+x[2]+x[3] })
dataset## ID StuName test asgn Prsnt Final q1 q2 q3 q4 total CW
## 1 60001 Ahmad 15 14 17 13 0.0 9 2 4.0 74 46
## 2 60003 Abu 26 13 18 22 3.0 5 8 6.0 101 57
## 3 60006 Samy 21 15 19 25 6.0 7 4 8.0 105 55
## 4 60008 Chong 25 10 17 14 2.0 3 4 5.0 80 52
## 5 60009 Paul 25 15 16 20 3.0 7 6 4.0 96 56
## 6 60011 John 18 15 19 22 4.0 7 4 7.0 96 52
## 7 60014 Devi 30 15 19 28 4.0 5 9 10.0 120 64
## 8 60015 Pillip 16 15 19 20 4.0 5 6 5.0 90 50
## 9 60023 Meilin 18 13 18 22 2.0 5 7 8.0 93 49
## 10 60025 Lily 30 14 18 24 5.5 6 5 7.5 110 62
## 11 60026 Jamil 12 10 12 12 1.0 5 1 6.0 59 34