S3 objects

  1. Following the cohort example in the notes, suppose we want to create a mean method.
  2. List all S3 methods associated with the mean function.
methods("mean")
  1. Examine the source code of mean.
body("mean")
  1. What are the arguments of mean?
args("mean")
  1. Create a function called mean.cohort that returns a vector containing the mean weight and mean height. Ensure that you can pass in the standard mean arguments, i.e.na.rm.`
mean.cohort = function(x, ...) {
  m1 = mean(x$details[, 1], ...)
  m2 = mean(x$details[, 2], ...)
  return(c(m1, m2))
}
  1. Let's now make a similar function for the standard deviation
  2. Look at the arguments of the sd function.
  3. Create an function call sd.cohort that returns a vector containing the weight and height standard deviation. Ensure that you can pass in the standard sd arguments, i.e.na.rm.`
  4. Create a default sd function. Look at cor.default in the notes for a hint.
sd = function(x, ...) UseMethod("sd")
sd.default = function(x, ...) stats::sd(x, ...)
sd.cohort = function(x, ...) {
  s1 = sd(x$details[, 1], ...)
  s2 = sd(x$details[, 2], ...)
  return(c(s1, s2))
} 
  1. Create a summary method for the cohort class. When the summary function is called on a cohort object it should call the base summary on the details element.
  2. Use the body function to check if the function is already a generic function.
  3. Use the args function to determine the arguments.
  4. Create a summary.cohort function
## summary is already a generic
body(summary)

## Match the args
args(summary)

## Function
summary.cohort = function(object, ...) summary(object$details, ...)
  1. Create a hist method for the cohort class. When the hist function is called on a cohort object, it should produce a single plot showing two histograms - one for height and another for weight.
## hist is already a generic
body(hist)

## Match the args
args(hist)

## Function
hist.cohort = function(x, ...) {
  op = par(mfrow = c(1, 2))
  hist(x$details[, 1], main = "Weight")
  hist(x$details[, 2], main = "Height")
  par(op)
}
  1. Create a [ method for the cohort class. This method should return a cohort object, but with the relevant rows sub setted. For example, if cc was a cohort object, then
cc[1:3, ]

\noindent would return the first three rows of the data frame.

## Lots of methods available. 
methods("[")

## Examine [.data.frame
args("[.data.frame")

"[.cohort" = function(x, ...) {
  x$details = x$details[...]
  x
}
  1. Create a [<- method for the cohort class. This method should allow us to replace values in the details data frame, i.e.
cc[1, 1] = 10
## Lots of methods available. 
methods("[<-")

## Examine [.data.frame
args("[<-.data.frame")

"[<-.cohort" = function(x, i, j, value) {
  x$details[i, j] = value
  x
}
cc[1:3, ] = 55

Solutions

Solutions are contained within the course package

library("jrAdvGgplot2")
vignette("solutions2", package = "jrAdvGgplot2")


jr-packages/jrAdvGgplot2 documentation built on Dec. 27, 2019, 2:23 p.m.