This is the solution to the “Your Turn” in class from October 7. Below is my function that accomplishes the following:

  1. Computes the mean of an input vector.
  2. Has error checking to ensure the input vector is numeric or logical, displays an error if not satisfied (see “stop”).
  3. Has a parameter to allow the user to specify if they want to exclude NA values.

Now we can loop over the columns of the diamonds data set and apply my function to all of the numeric columns.

library(ggplot2) #diamonds data
for(col in colnames(diamonds)) {
  dat <- diamonds[, col]
  if(all(class(dat) %in% c("numeric", "logical"))) { # sometimes class returns a vector of length > 1
    print(paste(col, ":", mymean(dat)))
  }
}
## [1] "carat : 0.797939747868001"
## [1] "depth : 61.749404894327"
## [1] "table : 57.457183908046"
## [1] "x : 5.73115721171672"
## [1] "y : 5.73452595476455"
## [1] "z : 3.53873377827215"

A potential problem with my function is if a user inputs a vector of length 0, the following occurs:

 mymean(numeric(0))
## [1] NaN

This returns a value of “Not a Number”, because we have divided by 0. While technically true, it might be nice to add some error handling to give the user a warning when this occurs.