Question 1

Read file marks1.csv

setwd('C:/Users/Administrator/Desktop')
marks1 <- read.csv(file = 'marks1.csv',head=TRUE,sep=',')
marks1
##        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

Using few available funcation to check the data frame

marks1 <- read.csv(file="marks1.csv", head=TRUE, sep=",")
columns_1 <- marks1[,c(1,3,5)]
columns_1
##        X test Prsnt
## 1  60001   15    17
## 2  60003   26    18
## 3  60006   21    19
## 4  60008   25    17
## 5  60009   25    16
## 6  60011   18    19
## 7  60014   30    19
## 8  60015   16    19
## 9  60023   18    18
## 10 60025   30    18
## 11 60026   12    12
columns_2 <- marks1[,3:6]
columns_2
##    test asgn Prsnt Final
## 1    15   14    17    13
## 2    26   13    18    22
## 3    21   15    19    25
## 4    25   10    17    14
## 5    25   15    16    20
## 6    18   15    19    22
## 7    30   15    19    28
## 8    16   15    19    20
## 9    18   13    18    22
## 10   30   14    18    24
## 11   12   10    12    12
marks3 <- marks1[marks1$test==25,]
marks3
##       X   X.1 test asgn Prsnt Final q1 q2 q3 q4
## 4 60008 Chong   25   10    17    14  2  3  4  5
## 5 60009  Paul   25   15    16    20  3  7  6  4
marks4 <- marks1[marks1$Final %in% c(22,20),]
marks4
##       X    X.1 test asgn Prsnt Final q1 q2 q3 q4
## 2 60003    Abu   26   13    18    22  3  5  8  6
## 5 60009   Paul   25   15    16    20  3  7  6  4
## 6 60011   John   18   15    19    22  4  7  4  7
## 8 60015 Pillip   16   15    19    20  4  5  6  5
## 9 60023 Meilin   18   13    18    22  2  5  7  8
marks5 <- marks1[with(marks1,test==25&Prsnt==17),]
marks5
##       X   X.1 test asgn Prsnt Final q1 q2 q3 q4
## 4 60008 Chong   25   10    17    14  2  3  4  5

Question 3

Check the names of the variables in the data frame

names(marks1)
##  [1] "X"     "X.1"   "test"  "asgn"  "Prsnt" "Final" "q1"    "q2"    "q3"   
## [10] "q4"

Question 4

Rename the first variable X to ID

colnames(marks1)[1] <- 'ID'
marks1
##       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

colnames(marks1)[2] <- 'StuName'
marks1
##       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

new_marks1 <- marks1[,c(-1,-2)]
new_marks1
##    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

Total <- apply(new_marks1,1,sum)
marks1 <- cbind(marks1,Total)
marks1
##       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

CW <- apply(new_marks1,1,function(x) {x[1]+ x[2]+ x[3]})
marks1 <- cbind(marks1,CW)
marks1
##       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