need help rewrite alternate R lang code of existing code R lang .
######### Q1 ############################ MSP<-c(seq(40,85,5)) MSP_course_marks<-matrix(0,5,65) for (i in 1:65) { for (j in 1:5) sample 5 times { marks<-sample(MSP,1,replace = TRUE) MSP_course_marks[j,i]<-marks } } rownames(MSP_course_marks)<-c("course1","course2","course3","course4","course5") for (k in 1:65) { assign(paste0("MSP_student_",k),MSP_course_marks[,k]) } for (l in 1:5) { assign(paste0("max_marks_in_course",l),max(MSP_course_marks[l,])) assign(paste0("min_marks_in_course",l),min(MSP_course_marks[l,])) } for (m in 1:65) { assign(paste0("max_marks_student",m),max(MSP_course_marks[,m])) assign(paste0("min_mark_by_student_",m),min(MSP_course_marks[,m])) } ## Transpose ## t(MSP_course_marks) row_matrix<-matrix(0,1,325) ######### Q5 ############################ #row-wise and column-wise sort Row_Matrix <- matrix(NA, 5, 65) Column_Matrix <- matrix(NA, 5, 65) for (r in 1:5) { Row_Matrix[r,] <- sort(MSP_course_marks[r,], decreasing=FALSE) } for (c in 1:65) { Column_Matrix[,c] <- sort(MSP_course_marks[,c], decreasing=FALSE) } Column_Matrix_Transpose <- t(Column_Matrix) Square_Matrix <- Row_Matrix %*% Column_Matrix_Transpose Principal_Diagonal_Sum <- 0 Principal_Diagonal_Product <- 1 for (i in 1:5) { Principal_Diagonal_Sum <- Principal_Diagonal_Sum + Square_Matrix[i,i] Principal_Diagonal_Product <- Principal_Diagonal_Product * Square_Matrix[i,i] } ######### Q6 ############################ #row-wise and column-wise mean and variance Row_Means <- matrix(NA, 5, 1) # average marks across each course Row_Variances <- matrix(NA, 5, 1) # measure of variability of marks in each course. # Higher the row-variance, higher the range of marks secured by students Column_Means <- matrix(NA, 1, 65) # average mark of each student across all courses Column_Variances <- matrix(NA, 1, 65) # measure of variability of marks secured by each student across each course # Higher the column-variance, higher the range