Files Corresponding to Short Course Programming in R
mySummary that takes in a
single vector and is capable of outputting the min, max,
median, mean, and sd of the vector.By default have the function produce the min, median, and max
only. Return the appropriate object with names for each item returned.
Allow for the passing of unnamed arguments.
Hint: Create an empty list using myList <- list(). If the user
specifies they want an input (or by default it is TRUE), add an
element to that list (myList$max <- for instance).
mySummary <- function(vector, min = TRUE, max = TRUE, med = TRUE, mean = FALSE, sd = FALSE, ...) {
myList <- list()
if (min == TRUE) {
myList$Minimum <- min(vector, ...)
}
if (max == TRUE) {
myList$Maximum <- max(vector, ...)
}
if (med == TRUE) {
myList$Median <- median(vector, ...)
}
if (mean == TRUE) {
myList$Mean <- mean(vector, ...)
}
if (sd == TRUE) {
myList$SD <- sd(vector, ...)
}
return(myList)
}
min, mean, and sd. (Recall the na.rm=TRUE argument for many
of the basic summary functions.x <- c(runif(10), NA)
mySummary(x, max = FALSE, med = FALSE, mean = TRUE, sd = TRUE, na.rm = TRUE)
## $Minimum
## [1] 0.2320222
##
## $Mean
## [1] 0.4887693
##
## $SD
## [1] 0.2720501