mean
method.mean
function.methods("mean")
mean
. body("mean")
mean
?args("mean")
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)) }
sd
function.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
.`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)) }
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.body
function to check if the function is already a generic function.args
function to determine the arguments.summary.cohort
function## summary is already a generic body(summary) ## Match the args args(summary) ## Function summary.cohort = function(object, ...) summary(object$details, ...)
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. You can either use base or ggplot2, again we'll be using ggplot2. To get both plots onto one plotting window, try using the grid.arrange()
function from gridExtra package.## hist is already a generic body(hist) ## Match the args args(hist) ## Function hist.cohort = function(x, ...) { dd = x$details Weight = ggplot(dd, aes(x = weight)) + geom_histogram() + labs(title = "Weight") Height = ggplot(dd, aes(x = height)) + geom_histogram() + labs(title = "Height") gridExtra::grid.arrange(Weight, Height) }
[
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, thencc[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 }
[<-
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 are contained within the course package
library("jrOOP") vignette("solutions2", package = "jrOOP")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.