R语言中自编函数(例题)
1、
mystats <- function(x, parametric = TRUE, print = FALSE){
if (parametric) {
center = mean(x); spread = sd(x)
}else
{
center <- median(x); spread = mad(x)
}
if (print & parametric) {
cat("Mean = ", center, "\n","\bSD = ", spread, "\n")
}else if (print & !parametric) {
cat("Median = ", center, "\n", "\bMAD = ", spread,"\n")
}
result <- list(center = center, spread = spread)
return(result)
}
x <- 1:5
mystats(x)
mystats(x,parametric = T,print = T)
mystats(x, parametric = F, print = T)