# More on creating functions ...

During class we reviewed the creation my way of reviewing the homework assignment. A set of possible responses is reproduced below.

The code we produced in class can be downloaded and reviewed here: Code-Day11.R

Consider two examples:

#### The min

``````my_min <- function(input) {

output <- sort(input, decreasing = FALSE)
return(output)

}

# test my_min() and compare to min()

x <- 10:1
y <- c(0, 1, -1)
z <- rnorm(n = 10, mean = 0, sd = 1)

min(x)
min(y)
min(z)

my_min(x)
my_min(y)
my_min(z)
``````

#### The mean

``````my_mean <- function(input) {

ouput <- sum(input)/length(input)
return(output)

}
``````

#### Even vs. odd

``````even <- function(input) {

output <- ifelse(input %% 2 == 0, "even", "odd")
return(output)

}
``````

#### Absolute value (Version 1)

``````my_abs <- function(input) {

output <- ifelse(input < 0, input * -1, input)
return(output)

}
``````

#### Absolute value (Version 2)

``````my_abs <- function(input) {

output <- sqrt(input^2)
return(output)

}
``````

#### Median

``````# this version depends on the even() function above

my_median <- function(input) {

n <- length(input)
x <- sort(input)

if (even(n) == "even") {

return(mean(x[n/2, n/2 + 1]))

} else {

return(x[ceiling(n/2)])

}

}

# test my_median() on even and odd length inputs
# and compare to median()

x <- rnorm(100)
y <- rnorm(101)

median(x)
my_median(x)

median(y)
my_median(y)
``````