Question: 5: Let's apply some functions(a) Writing trimmed mean function Write a function that calculates the mean of a numeric vectorx, ignoring thessmallest andllargest values (this

5: Let's apply some functions(a) Writing trimmed mean function

Write a function that calculates the mean of a numeric vectorx, ignoring thessmallest andllargest values (this is atrimmed mean).

E.g., ifx = c(1, 7, 3, 2, 5, 0.5, 9, 10),s = 1, andl = 2, your function would return the mean ofc(1, 7, 3, 2, 5)(this isxwith the 1 smallest value (0.5) and the 2 largest values (9, 10) removed).

Your function should use thelength()function to check ifxhas at leasts + l + 1values. Ifxis shorter thans + l + 1, your function should use themessage()function to tell the user that the vector can't be trimmed as requested. Ifxis at least lengths + l + 1, your function should return the trimmed mean.

# Here's a function skeleton to get you started # Fill me in with an informative comment # describing what the function does trimmedMean <- function(x, s = 0, l = 0) { # Write your code here } 

Hint:For this exercise it will be useful to recall thesort()function that you first saw in Lecture 1.

Note:Thes = 0andl = 0specified in the function definition are the default settings. i.e., this syntax ensures that ifsandlare not provided by the user, they are both set to0. Thus the default behaviour is that thetrimmedMeanfunction doesn't trim anything, and hence is the same as themeanfunction.

(b) Apply your function with a for loop
set.seed(201802) # Sets seed to make sure everyone's random vectors are generated the same list.random <- list(x = rnorm(50), y = rexp(65), z = rt(100, df = 1.5)) # Here's a Figure showing histograms of the data par(mfrow = c(1,3)) hist(list.random$x, breaks = 15, col = 'grey') hist(list.random$y, breaks = 10, col = 'forestgreen') hist(list.random$z, breaks = 20, col = 'steelblue') 

Using afor loopand your function from part(a), create a vector whose elements are the trimmed means of the vectors inlist.random, takings = 5andl = 5.

# Edit me 
(c) Calculate the un-trimmed means for each of the vectors in the list. How do these compare to the trimmed means you calculated in part (b)? Explain your findings.
# Edit me 

Explanation:

Replace this text with your answer. (do not delete the html tags)

(d) lapply(), sapply()

Repeat part(b), using thelapplyandsapplyfunctions instead of a for loop. Yourlapplycommand should return a list of trimmed means, and yoursapplycommand should return a vector of trimmed means.

# Edit me 

Hintlapplyandsapplycan take arguments that you wish to pass to thetrimmedMeanfunction. E.g., if you were applying the functionsort, which has an argumentdecreasing, you could use the syntaxlapply(..., FUN = sort, decreasing = TRUE).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!