Can someone please help me with this R-Program coding question? Thanks so much i
ID: 3667397 • Letter: C
Question
Can someone please help me with this R-Program coding question? Thanks so much in advance!
Write a function that calculates the geometric mean of a vector x = (x-1,... x_n) and returns an error message if any element of x is negative. 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=Iist(NULL, paste 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. 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()).Explanation / Answer
Write a function which goes through every entry in a list, checks whether it is a
character vector (is.character()), and if so prints it (print() or cat()).
There are various possibilities, here is one:
> printChar = function(lst) {
+ for (i in lst) {
+ if (is.character(i))
+ print(i)
+ }
+ }
This can be done more neatly using sapply()
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.
> summarize = function(x) {
+ list(mean = mean(x), median = median(x), variance = var(x))
+ }
[Hint: If you're not sure what the name of a function is, try using fuzzy search:
e.g. ??variance.]