Question

In: Computer Science

this code should be in R. Write function(s) to drop the smallest one exm grade out...

this code should be in R.

Write function(s) to drop the smallest one exm grade out of several Exm grades. If a student misses an exm, the grade is NA. You must use NA instead of 0 to represent the value for exms missing. (a) Develop a function to convert an array to another array in which all NA grades are replaced with zero. (b) Complete your function to compute the mean of all elements row by row after dropping exactly one lowest grade. You cannot use any loop statement inside or outside your functions for both (a) and (b). You may use the built-in function in R to find the minimum value in an array. You may use ifelse() only once. You may use the built-in function mean() to find the mean. But be careful! For testing purpose, you may use the following exm grades for each student in each row:

            Exm 1          Exm 2         Exm 3          Exm 4

               80                  NA                90                  NA

               60                  80                  100                NA

               90                  80                  100                40

The average grade of each student must be displayed as a row in a matrix. For the exmple above, you need to display a 3 x 1 matrix with three rows of mean values in one column.

You may use functions: is.na() or na.omit().

Solutions

Expert Solution

Below is the screenshot of code and output:

Below is the R code for same. As mentioned in the question loops have not been used. For part a, is.na() function is used and for part b, apply() function is used. The implemented functions are also tested against the example given in the question.

#implementing function for part a to replace NAs with 0
replace_NA <- function(data) {
data[is.na(data)] <- 0
return(data)
}
#helper function for part b to find avg of a row after dropping minimum grade
mean_after_removing_min <- function(data) {
return((sum(data)-min(data))/(length(data)-1))
}
#implementing required function for part b to return an avg matrix after removing minimum grade
average_grade_after_dropping_min <- function(data) {
return(matrix(apply(data,1,mean_after_removing_min),dimnames =list(c(unlist(dimnames(result)[1])),c("Avg grade")) ))
}

#TESTING
column.names <- c("Exm 1","Exm 2","Exm 3","Exm 4")
row.names <- c("Student_1","Student_2","Student_3")
matrix.names <- c("Exam Grades for students")
result <- array(c(80,60,90,NA,80,80,90,100,100,NA,NA,40),dim = c(3,4,1),dimnames = list(row.names,column.names,matrix.names))
print(result)# printing test case example

print("Now replacing NA's with 0s")
result = replace_NA(result)#Now replacing NA's with 0s
print(result)#printing modified array

#printing matrix containing averages computed for each student post dropping lowest grade
print(average_grade_after_dropping_min(result))


Related Solutions

Write a function in R named counts. This function should take as parameters a numeric vector...
Write a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x),max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x),b1),I2 = [b1,b − 2),...,In = (bn−1,max(x)]. Note that since the intervals are equally sized, the value of bi is constrained. The function will then return a...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
a) Let S ⊂ R, assuming that f : S → R is a continuous function,...
a) Let S ⊂ R, assuming that f : S → R is a continuous function, if the image set {f(x); x ∈ S} is unbounded prove that S is unbounded. b) Let f : [0, 100] → R be a continuous function such that f(0) = f(2), f(98) = f(100) and the function g(x) := f(x+ 1)−f(x) is equal to zero in at most two points of the interval [0, 100]. Prove that (f(50) − f(49))(f(25) − f(24)) >...
#python #code #AP class #Tech write a function code script which will print out number pyramid...
#python #code #AP class #Tech write a function code script which will print out number pyramid in the form of * so the output will be made up of **** resting on top of each other to form a pyramid shape. Bottom layer should be made of 5 multiplication signs like ***** then next 4 multiplication signs and so on. Top part should have only one *
Write a function that takes a valid stringlist and returns the index of the smallest element...
Write a function that takes a valid stringlist and returns the index of the smallest element in the list represented by the stringlist. You may not use split(). Examples: >>> stringlist min index('[123,53,1,8]') # 1 is smallest2 >>> stringlist min index('[1,2,345,0]') # 0 is smallest3 >>> stringlist min index('[5] ') # 5 is smallest0
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
Write a function for checking the speed of drivers. This function should have one parameter: speed....
Write a function for checking the speed of drivers. This function should have one parameter: speed. 1. If speed is less than 70, it should print “Ok”. 2. Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: "Points: 2". 3. If the driver gets more than 12 points, the function should print: “License suspended”...
The following should be performed using R and the R code included in your submission. To...
The following should be performed using R and the R code included in your submission. To obtain first prize in a lottery, you need to correctly choose n different numbers from N and 1 number from 20, known as the supplementary. That is we first draw n numbers from 1:N without replacement and then 1 number from 1:20 in another draw. Suppose n=7 and N=35. Let X be the number of drawn numbers that match your selection, where the supplementary...
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest...
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest value in the array. If the array is empty, return the “end” pointer ·and takes as parameters: (1)   a pointer to double. The pointer is the address of the start of an array, (2)   the “end” pointer to the address after the array (3)   and the address of the smallest value seen so far ·Write main() to test this function – try a case where the array...
Problem 3: Minimum In this problem, we will write a function to find the smallest element...
Problem 3: Minimum In this problem, we will write a function to find the smallest element of a list. We are, in a sense, reinventing the wheel since the min() function already performs this exact task. However, the purpose of this exercise is to have you think through the logic of how such a function would be implemented from scratch. Define a function named minimum(). The function should accept a single parameter named x, which is expected to be a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT