October 2017
Beginner to intermediate
236 pages
7h 38m
English
Let's give a name to the function that you are going to write here, desStat, and define its action as follows:
desStat <- function(numVec, type="classical"){ if(!is.numeric(numVec)) stop("The input must be numeric") average <- mean(numVec, na.rm = T) std <- sd(numVec, na.rm = T) med <- median(numVec, na.rm = T) mad <- mad(numVec, na.rm = T) descriptiveStats <- setClass("descriptiveStats", slots=list(centre="numeric", spread="numeric")) if(type=="classical") return(new("descriptiveStats", centre=average, spread=std)) if(type=="robust") return(new("descriptiveStats", centre=med, spread=mad)) } desStat(x) An object of class "descriptiveStats" Slot "centre": [1] 19.8 Slot "spread": [1] 5.256677