范例代碼如下:
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", "SD=", spread, "\n")}
else if (print & !parametric)
{cat("Median=", center, "\n", "MAD=", spread, "\n")}
result <- list(center=center, spread=spread)
return(result)
}
這幾行代碼不是很明白:
1.parametirc=TRUE
2.if (parametric)
3.if (print & parametric).
parametric=TRUE是指mystats這個函數有個參數叫parametric,並且它的默認值為TRUE
if (parametric)就是當parametric為TRUE的時候要做的事
if (print & parametric)就是當print和parametric都為TRUE的時候要做的事