1: my_mean
Stats 20 Midterm 1 Practice Questions
Spring 2021
The sample mean for a sample of values x = x1,x2,…,xn is defined as 1 n
Copyright By PowCoder代写 加微信 powcoder
x ̄=n xi i=1
Write a function called my_mean() that computes the mean of a numeric vector x of data values without using the mean() function. Include an optional logical na.rm argument with a default value of FALSE that specifies whether to remove NA values from the computation.
2: Incorrect my_sign
Observe the following function:
my_sign <- function(x) { if (x > 0) {
“positive”
if (x < 0) {
"negative"
if (x == 0) {
What is the intended purpose of my_sign()? For what inputs x will there be no output of my_sign()? (b)
Keeping the overall if statement structure (no additional if/else statements), edit my_sign() so that it works as intended.
Give example inputs that would cause my_sign() to throw: • an error
• one or more warnings
Using the fixed my_sign() function from (b), with a single command (e.g. one line of code), produce the output c("positive", "negative", "zero")
3: my_cumfun
Write a function called my_cumfun() that computes the cumulative fun of a numeric vector x. The out- put of my_cumfun(x, sum), my_cumfun(x, prod), my_cumfun(x, max), my_cumfun(x, min) should be identical to the output of cumsum(x), cumprod(x), cummax(x), cummin(x) respectively.
4: is.mode
is.logical() returns TRUE if the storage mode of the input x is logical and FALSE otherwise. is.numeric() and is.character() work similarly. Write a function called is.mode() which takes as inputs x: an object, and mode: a length 1 character vector. The output of is.mode(x, mode) should be TRUE only if x has storage mode equal to the mode argument.
Note: each part would be considered “one” question on the exam. So question 2 is really four questions.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com