Part - A

#1. Create a matrix StudentMarks which stores the marks scored by 5 students in 4 courses.
marks=c(56,34,90,45,22,66,90,97,55,48,85,85,84,87,56,32,68,95,49,50)
StudentMarks = matrix(marks,nrow=5,ncol=4,byrow=TRUE,dimnames=list(c("Kaarthika","Harry","Hermoine","Alexa","Ron"),c("c1","c2","c3","c4")))
StudentMarks 
##           c1 c2 c3 c4
## Kaarthika 56 34 90 45
## Harry     22 66 90 97
## Hermoine  55 48 85 85
## Alexa     84 87 56 32
## Ron       68 95 49 50
#Find the class average of each course.
colMeans(StudentMarks)
##   c1   c2   c3   c4 
## 57.0 66.0 74.0 61.8
#3. Find the total score of each student.
rowSums(StudentMarks)
## Kaarthika     Harry  Hermoine     Alexa       Ron 
##       225       275       273       259       262
#4. Print the marks scored by all the students in subject 1 and subject4.
colSums(StudentMarks[,c(1,4)])
##  c1  c4 
## 285 309
#5. Include the marks scored by a new student in all the courses.
Angel=c(55,89,67,34)
rbind(StudentMarks,Angel)
##           c1 c2 c3 c4
## Kaarthika 56 34 90 45
## Harry     22 66 90 97
## Hermoine  55 48 85 85
## Alexa     84 87 56 32
## Ron       68 95 49 50
## Angel     55 89 67 34

PART-B

#6. Create a matrix A with sequence of numbers from 21 to 32 in row major with 3 rows.
A = matrix(c(21:32),3,byrow=TRUE)
A
##      [,1] [,2] [,3] [,4]
## [1,]   21   22   23   24
## [2,]   25   26   27   28
## [3,]   29   30   31   32
#7 Check whether A is symmetric
isSymmetric(StudentMarks)
## [1] FALSE
#8. Create matrix B by slicing only the first three columns of Matrix A
B = A[,c(1:3)]
B
##      [,1] [,2] [,3]
## [1,]   21   22   23
## [2,]   25   26   27
## [3,]   29   30   31
#9. Find the mean, determinant and rank of Matrix B
mean(B)
## [1] 26
det(B)
## [1] -4.263256e-14
qr(B)
## $qr
##             [,1]        [,2]          [,3]
## [1,] -43.6692111 -45.3866683 -4.710413e+01
## [2,]   0.5724857   0.2243677  4.487353e-01
## [3,]   0.6640834   0.9743183  2.609024e-15
## 
## $rank
## [1] 2
## 
## $qraux
## [1] 1.480888e+00 1.225175e+00 2.609024e-15
## 
## $pivot
## [1] 1 2 3
## 
## attr(,"class")
## [1] "qr"
#10. Find the trace of Matrix B
print(sum(diag(B)))
## [1] 78

PART - C

#11. Create a matrix by following the statements
#Create a vector named Rohith with three values.
# Create a vector named Bharath with three values.
# Create a vector named Kevin with three values.

Rohith = c(45,66,35)
Bharath = c(78,34,77)
Kevin = c(90,56,98)
#From the above three vectors construct a row major matrix named Marks with 3 rows.
#Print the Marks matrix
Marks = matrix(c(Rohith, Bharath, Kevin),nrow = 3,byrow=TRUE)
print(Marks)
##      [,1] [,2] [,3]
## [1,]   45   66   35
## [2,]   78   34   77
## [3,]   90   56   98
#Create a vector named Names with values - Rohith, Bharath and Kevin
Names <- c("Rohith","Bharath","Kevin")
print(Names)
## [1] "Rohith"  "Bharath" "Kevin"
#Create a vector named Headings with values - CAT-1, CAT-2, FAT
Headings = c("CAT1","CAT2","FAT")
print(Headings)
## [1] "CAT1" "CAT2" "FAT"
#Assign Names vector as row names to the Marks Matrix
#Assign Headings vector as column names to the Marks Matrix
#Print the Marks matrix
rownames(Marks) = Names
colnames(Marks) = Headings
print(Marks)
##         CAT1 CAT2 FAT
## Rohith    45   66  35
## Bharath   78   34  77
## Kevin     90   56  98
#12 Find the following from the above matrix.
#Find the total score of each person and name it as Total
Total = rowSums(Marks)
print(Total)
##  Rohith Bharath   Kevin 
##     146     189     244
#Add that column to the existing matrix and name the new matrix as Final_Marks
Marks <-cbind(Marks, Total)
Marks
##         CAT1 CAT2 FAT Total
## Rohith    45   66  35   146
## Bharath   78   34  77   189
## Kevin     90   56  98   244
#Find the average score of all the components of Final_marks matrix(CAT-1, CAT-2, FAT and Total). Round it to 2 decimal places.[Note: use round function]
Column_Means <- colMeans(Marks)
print(Column_Means)
##  CAT1  CAT2   FAT Total 
##    71    52    70   193
rounded_means <- round(Column_Means,2)
print(rounded_means)
##  CAT1  CAT2   FAT Total 
##    71    52    70   193
#Append the avg score in Final_Marks
Marks = rbind(Marks,rounded_means)
Marks
##               CAT1 CAT2 FAT Total
## Rohith          45   66  35   146
## Bharath         78   34  77   189
## Kevin           90   56  98   244
## rounded_means   71   52  70   193

PART - D

#Create a data frame with 6 students and the marks scored by them in 5 different courses.
#Each course has a maximum score of 100. If a student is present for the exam, its entry contains the score value and 0 otherwise.
students = c("Kaarthika","Praba","Divya","Sudharsha","Thiru","Ramya")
c1 = c(87,34,0,56,35,90)
c2 = c(56,78,96,0,67,45)
c3 = c(89,56,87,99,34,89)
c4 = c(23,65,45,83,90,95)
c5 = c(67,84,49,0,76,20)

df = data.frame(Name = students, C1=c1,C2=c2,C3=c3,C4=c4,C5=c5)
#View the contents of the data frame.
View(df)
#Find the total score of each student.
#Append a column to include the total score of the students and view the data frame.
df$Total = rowSums(df[,c("C1","C2","C3","C4","C5")])
View(df)
#Find the maximum score and display the name of the student who scored it.
print(df$Name[which.max(df$Total)])
## [1] "Ramya"
#Compute the average score of each course and append it as a new row in the data frame.
df <- rbind(df, c("Avg", colMeans(df[, -c(1, ncol(df))]), NA))

print(df)
##        Name               C1 C2               C3               C4
## 1 Kaarthika               87 56               89               23
## 2     Praba               34 78               56               65
## 3     Divya                0 96               87               45
## 4 Sudharsha               56  0               99               83
## 5     Thiru               35 67               34               90
## 6     Ramya               90 45               89               95
## 7       Avg 50.3333333333333 57 75.6666666666667 66.8333333333333
##                 C5 Total
## 1               67   322
## 2               84   317
## 3               49   277
## 4                0   238
## 5               76   302
## 6               20   339
## 7 49.3333333333333  <NA>
df[, 2:6] <- lapply(df[, 2:6], as.numeric)
#Access the scores of students in course3 using the column name.
print(df$C3)
## [1] 89.00000 56.00000 87.00000 99.00000 34.00000 89.00000 75.66667
#Use index number to retrieve the same data.
print(df[,4])
## [1] 89.00000 56.00000 87.00000 99.00000 34.00000 89.00000 75.66667
#Extract the score of third student in course4.
print(df[3,5])
## [1] 45
#Extract the scores of the first and second student in all the courses.
print(df[c(1,2),])
##        Name C1 C2 C3 C4 C5 Total
## 1 Kaarthika 87 56 89 23 67   322
## 2     Praba 34 78 56 65 84   317
#Display the names and total scores of all students.
print(df[,c(1,7)])
##        Name Total
## 1 Kaarthika   322
## 2     Praba   317
## 3     Divya   277
## 4 Sudharsha   238
## 5     Thiru   302
## 6     Ramya   339
## 7       Avg  <NA>