0

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

13th Jan 2022, 6:38 PM
kaushik gaekwad
kaushik gaekwad - avatar
2 Answers
+ 1
Please put the code in a script on sololearn playground. In the description the code is hard to read. You can start by replacing the loops with vecorized operations.
13th Jan 2022, 6:46 PM
Lisa
Lisa - avatar
0
1. Create a vector named MSP. The size of the vector should be 10 and the elements in the vector are multiples of 5 arranged sequentially (starting from 40). Hint: Use the sequence function. 2. Randomly select 5 numbers from the vector. Repeat the process 65 times (i.e. create 65 samples with each sample containing 5 numbers) and name these vectors as MSP_Student_i, where i refers to the sample number. Hint: Use the sample function 3. Create a matrix named MSP_courses_marks by combining these vectors column-wise. Name the rows as Course_i, where i refers to the row number. Find the max and min in each row and column and interpret these numbers. 4. Transpose the matrix. What does the rows and columns represent now? 5. Create two new matrices named Row Matrix and Column Matrix from the MSP_courses_marks. The elements of these matrices are the MSP_course_marks elements sorted row-wise and column-wise respectively. Also find the sum and the product of the principal diagonal elements and interpret these numbers. 6. Compute the column-wise and row-wise mean and variances for the MSP_courses_marks and explain what do they represent? 7. Transform the elements in the MSP_courses_marks using the following transformation: 𝑓 (đ‘„) = 2 ∗ 𝑒−𝜆𝑖 ∗ 𝜆! " +đ‘„! Where λi represents the mean score in course i. You are required to use both for loop as well as your own function. 8. Plot x and f(x). What can you conclude about the shape of the curve/plot? 9. How will the matrix MSP_courses_marks be modified if Student 11 drops out of the program? What if a new student joins the program and has the following scores: (55,60,75,80,70) ? 10. Now create a dataframe called MSP_DataFrame with the following elements: a. MSP_names: A vector with the names of all the students in the program b. MSP_courses_marks: The matrix that you had created earlier. c. MSP_experience: A logical vector with data as either TRUE or FALSE Now access/print the name, experience and marks of Student_7 and Student_16 from the datafra
13th Jan 2022, 7:13 PM
kaushik gaekwad
kaushik gaekwad - avatar