### ----------------------------------------------------------------------- ### Programmieren mit statistischer Software (SoSe 2017) ### ----------------------------------------------------------------------- ## ----fun1---------------------------------------------------------------- # name <- function(arg_1, arg_2, ...) { # statements # } ## ----fun2---------------------------------------------------------------- # name(expr_1, expr_2) ## ----fun3---------------------------------------------------------------- # ?which.max which_maxdev <- function(x) { mdn <- median(x) devs <- abs(x - mdn) which.max(devs) } ## ----fun4---------------------------------------------------------------- data("Forbes2000", package = "HSAUR2") # summary(Forbes2000$sales) which_maxdev(Forbes2000$sales) # Forbes2000$sales[which_maxdev(Forbes2000$sales)] # summary(Forbes2000$marketvalue) which_maxdev(Forbes2000$marketvalue) # Forbes2000$marketvalue[which_maxdev(Forbes2000$marketvalue)] ## ----fun5---------------------------------------------------------------- which_maxdev <- function(x) { mdn <- median(x) devs <- abs(x - mdn) which.max(devs) print("Hallo") } which_maxdev(Forbes2000$sales) ## ----fun6---------------------------------------------------------------- which_maxdev <- function(x) { mdn <- median(x) devs <- abs(x - mdn) return(which.max(devs)) print("Hallo") } which_maxdev(Forbes2000$sales) ## ----fun7---------------------------------------------------------------- which_maxdev <- function(x) { mdn <- median(x) devs <- abs(x - mdn) which.max(devs) } which_maxdev(Forbes2000$profits) ## ----fun8---------------------------------------------------------------- median(Forbes2000$profits) args(median) ## ----fun9---------------------------------------------------------------- which_maxdev <- function(x, ...) { mdn <- median(x, ...) devs <- abs(x - mdn) which.max(devs) } which_maxdev(Forbes2000$profits, na.rm = TRUE) ## ----fun10--------------------------------------------------------------- which_maxdev <- function(x, na.rm = TRUE) { mdn <- median(x, na.rm = na.rm) devs <- abs(x - mdn) which.max(devs) } which_maxdev(Forbes2000$profits) ## ----fun11--------------------------------------------------------------- which_maxdev(Forbes2000$sales) which_maxdev(Forbes2000$sales, FALSE) # which_maxdev(FALSE, Forbes2000$sales) # funktioniert nicht ## ----fun12--------------------------------------------------------------- which_maxdev(x = Forbes2000$sales, na.rm = FALSE) which_maxdev(na.rm = FALSE, x = Forbes2000$sales) which_maxdev(na.rm = FALSE, Forbes2000$sales) # teilweise Benennung funktioniert auch ## ----match1-------------------------------------------------------------- which_maxdev(Forbes2000$sales, na.rm = FALSE) # Jeder Argument-Name darf nur einmal verwendet werden # which_maxdev(na.rm=Forbes2000$sales, na.rm = FALSE) # funktioniert nicht ## ----match2-------------------------------------------------------------- # hier: n statt na.rm which_maxdev(Forbes2000$sales, n = FALSE) # which_maxdev(Forbes2000$sales, n = FALSE, na = FALSE) # funktioniert nicht ## ----match3-------------------------------------------------------------- which_maxdev(n = FALSE, Forbes2000$sales) # Unmatched Arguments -> error which_maxdev(Forbes2000$sales, foo = 1) ## ----check1-------------------------------------------------------------- which_maxdev(Forbes2000$name) which_maxdev(as.matrix(Forbes2000[, c("sales", "marketvalue")])) which_maxdev(Forbes2000$sales, na.rm = "yes") ## ----check2-------------------------------------------------------------- which_maxdev <- function(x, na.rm = TRUE) { stopifnot(is.numeric(x)) stopifnot(is.vector(x)) stopifnot(is.logical(na.rm)) mdn <- median(x, na.rm = na.rm) devs <- abs(x - mdn) which.max(devs) } which_maxdev(Forbes2000$name) which_maxdev(as.matrix(Forbes2000[, c("sales", "marketvalue")])) which_maxdev(Forbes2000$sales, na.rm = "yes") ## ----check3-------------------------------------------------------------- # mit apply() apply(as.matrix(Forbes2000[, c("sales", "marketvalue")]), 2, which_maxdev) ## ----return1------------------------------------------------------------- which_maxdev <- function(x, na.rm = TRUE) { stopifnot(is.numeric(x) & is.vector(x)) stopifnot(is.logical(na.rm)) mdn <- median(x, na.rm = na.rm) devs <- abs(x - mdn) return(which.max(devs)) } ## ----return2------------------------------------------------------------- which_maxdev(Forbes2000$sales) res <- which_maxdev(Forbes2000$sales) res ## ----return3------------------------------------------------------------- which_maxdev <- function(x, na.rm = TRUE) { stopifnot(is.numeric(x) & is.vector(x)) stopifnot(is.logical(na.rm)) mdn <- median(x, na.rm = na.rm) devs <- abs(x - mdn) invisible(which.max(devs)) } ## ----return4------------------------------------------------------------- which_maxdev(Forbes2000$sales) res <- which_maxdev(Forbes2000$sales) res ## ----scope1-------------------------------------------------------------- f <- function(x) { y <- 2 * x cat("x =", x, "\n") cat("y =", y, "\n") cat("z =", z, "\n") } ## ----scope2-------------------------------------------------------------- # Fehlermeldung, da z nicht definiert ist f(2) ## ----scope3-------------------------------------------------------------- # Definiton von z z <- 42 f(2) # Warnung: Die Verwendung freier Variablen in einer Funktion # ist sehr fehleranfaellig # und sollte deshalb vermieden werden.