Question: 2. (a) Write a function that calculates the geometric mean of a vector x = (x1, . . . , xn) and returns an error
2. (a) Write a function that calculates the geometric mean of a vector x = (x1, . . . , xn) and returns an error message if any element of x is negative. (b) Apply your function to each column of the dataset given below. Some of the columns have data entry errors and contain negative numbers. Create a labeled output vector that gives the geometric mean for valid data and NA for columns with invalid data.
(Hint use try()). set.seed(123) data <- matrix(rnorm(10000, mean=3), ncol=25, dimnames=list(NULL, paste("X", 1:25, sep=".")))
(c) Write a function which takes a numeric vector x, and returns a named list containing the mean, median and variance of the values in x. (d) Write a function which goes through every entry in a list, cheks whether it is a character vector (is.character()), and if so prints it (print() or cat()).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
