Functions

Outline

  1. Loops
  2. Functions

Loops - For Loops

for (i in 1:5) {
  print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
for (i in 1:8) {
  print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8

For Loops Examples

x <- 0
for(i in 1:5){
  x <- c(x,i)
}
x
## [1] 0 1 2 3 4 5

For Loops Examples

x <- 1
for(i in 1:5){
  x <- c(x,i)
}
x
## [1] 1 1 2 3 4 5
x <- 12
for(i in 1:5){
  x <- c(x,i)
}
x
## [1] 12  1  2  3  4  5
x <- 12
for(i in 1:5){
  x <- c(x,i)
}
x
## [1] 12  1  2  3  4  5

Nested Loops Examples

for (i in 1:3) {
  for (j in 1:2) {
    print(i * j)
  }
}
## [1] 1
## [1] 2
## [1] 2
## [1] 4
## [1] 3
## [1] 6

Nested Loops Examples

for (i in 1:3) {
  for (j in 1:2) {
    print(i * j^2)
  }
}
## [1] 1
## [1] 4
## [1] 2
## [1] 8
## [1] 3
## [1] 12

Nested Loops Examples

y <- 2
for(i in 1:5){
  y <- y*i
}
y
## [1] 240

Nested Loops Examples

y <- 2
for(i in 1:20){
  y <- y*i
}
y
## [1] 4.865804e+18

Nested Loops Examples

y <- 1
x <- 0
for(i in 1:10){
  y <- y*i
  print(y)
}
## [1] 1
## [1] 2
## [1] 6
## [1] 24
## [1] 120
## [1] 720
## [1] 5040
## [1] 40320
## [1] 362880
## [1] 3628800
y
## [1] 3628800

Filling in a Vector

x <- rep(NA, 5)
x
## [1] NA NA NA NA NA
# Inefficient
x <- rep(NA, 5)
for (i in 1:5) {
  x[i] <- i * 2
}
x
## [1]  2  4  6  8 10
# Much better
x <- seq(2, 10, by = 2)
x
## [1]  2  4  6  8 10

Filling in a Matrix

A <- c(1,1,1)
for(j in 1:10){
A <- cbind(j,j^2, 5*j)
}
A
##       j       
## [1,] 10 100 50
A <- c(1,1,1)
for(j in 1:10){
A <- rbind(j,j^2, 5*j)
}
A
##   [,1]
## j   10
##    100
##     50

Filling in a Matrix

A <- c(1,1,1)
for(j in 1:10){
A = cbind(A, c(j,j^2, 5*j))
}
A
##      A                              
## [1,] 1 1  2  3  4  5  6  7  8  9  10
## [2,] 1 1  4  9 16 25 36 49 64 81 100
## [3,] 1 5 10 15 20 25 30 35 40 45  50
A <- c(1,1,1)
for(j in 1:10){
A = rbind(A, c(j,j^2, 5*j))
}
A
##   [,1] [,2] [,3]
## A    1    1    1
##      1    1    5
##      2    4   10
##      3    9   15
##      4   16   20
##      5   25   25
##      6   36   30
##      7   49   35
##      8   64   40
##      9   81   45
##     10  100   50

Filling in a Matrix

x <- matrix(NA, nrow = 4, ncol = 3)
for (i in 1:4) {
  for (j in 1:3) {
    x[i, j] <- i * j
  }
}
x
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    2    4    6
## [3,]    3    6    9
## [4,]    4    8   12

Filling in a Matrix

x <- matrix(rep(1:3, 4), nrow = 4, ncol = 3, byrow = TRUE)
x
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    1    2    3
## [3,]    1    2    3
## [4,]    1    2    3
x * 1:4
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    2    4    6
## [3,]    3    6    9
## [4,]    4    8   12

while Loop Example

x <- 10
while(x<30){
x <- x+5
print(x)
}
## [1] 15
## [1] 20
## [1] 25
## [1] 30
x
## [1] 30

while Loop Example

x <- 5
while(x<20){
x <- x+4
print(x)
}
## [1] 9
## [1] 13
## [1] 17
## [1] 21
x
## [1] 21

while Loop Example

x <- 5
y <- x
while(x<20){
x <- x+4
y <- c(y,x)
print(y)
}
## [1] 5 9
## [1]  5  9 13
## [1]  5  9 13 17
## [1]  5  9 13 17 21
x
## [1] 21
y
## [1]  5  9 13 17 21

while Loop Example

set.seed(3)
x <- -1
while (x < 0) {
  x <- rnorm(1)
  print(x)
}
## [1] -0.9619334
## [1] -0.2925257
## [1] 0.2587882
x
## [1] 0.2587882

while Loop Example

set.seed(3)
x <- -1
while (x < -0.5) {
  x <- rnorm(1)
  print(x)
}
## [1] -0.9619334
## [1] -0.2925257
x
## [1] -0.2925257

Exercise 1

x <- matrix(NA, nrow = 4, ncol = 4)
for (i in 1:4) {
  for (j in 1:4) {
    x[i, j] <- i * j^2
  }
}
x
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    9   16
## [2,]    2    8   18   32
## [3,]    3   12   27   48
## [4,]    4   16   36   64

Exercise 2

x0 <- c(1,1)
for(i in 1:10){
x0 <- c(x0, x0[i+1]+x0[i])
}
x0
##  [1]   1   1   2   3   5   8  13  21  34  55  89 144

Exercise 3

y0 <- 5
y0_trace <- y0
for(i in 1:6){
y0 <- -0.5 * (y0-3) + y0
y0_trace <- c(y0_trace, y0)
}
y0_trace
## [1] 5.00000 4.00000 3.50000 3.25000 3.12500 3.06250 3.03125
y0
## [1] 3.03125

Functions

Anatomy of a function

function_name <- function(param1, param2 = "default") {
  # Body of the function
  return(output)
}

Temperature Conversion

fahr_to_kelvin <- function(fahr) {
  # function that converts temperature in degrees Fahrenheit to kelvin
  # input: fahr: numeric value representing temp in degrees farh
  # output: kelvin: numeric converted temp in kelvin
  kelvin <- ((fahr - 32) * (5 / 9)) + 273.15
  return(kelvin)
}

fahr_to_kelvin(32)
## [1] 273.15

Temperature Conversion

fahr_to_kelvin <- function(fahr) {
  # function that converts temperature in degrees Fahrenheit to kelvin
  # input: fahr: numeric value representing temp in degrees farh
  # output: kelvin: numeric converted temp in kelvin
  kelvin <- ((fahr - 32) * (5 / 9)) + 273.15
  return(kelvin)
}

fahr_to_kelvin(212)
## [1] 373.15

Functions: More Examples

## Square a number, add 2
square_plus_2 <- function(x) {
  y <- x^2 + 2
  return(y)
}

square_plus_2(4)
## [1] 18
square_plus_2(10)
## [1] 102
square_plus_2(1:5)
## [1]  3  6 11 18 27

User-defined Function

## Create a function to print squares of numbers in sequence.
new.function <- function(a) {
   for(i in 1:a) {
      b <- i^2
      print(b)
   }
}   
new.function(3)
## [1] 1
## [1] 4
## [1] 9

User-defined Function

## Create a function to print squares of numbers in sequence.
new.function <- function(a) {
   for(i in 1:a) {
      b <- i^2
      print(b)
   }
}   
new.function(6)
## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25
## [1] 36

Calling a Function without an Argument

# Create a function without an argument.
new.function <- function() {
   for(i in 1:5) {
      print(i^2)
   }
}   

# Call the function without supplying an argument.
new.function()
## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25

Calling a Function with Argument Values

# Create a function with arguments.
new.function <- function(a,b,c) {
   result <- a * b + c
   print(result)
}

# Call the function by position of arguments.
new.function(5,3,11)
## [1] 26
# Call the function by names of the arguments.
new.function(a = 11, b = 5, c = 3)
## [1] 58

Creating The Quadratic Formula Function in R

quadraticRoots <- function(a, b, c) {

  print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))

  discriminant <- (b^2) - (4*a*c)

  if(discriminant < 0) {
    return(paste0("This quadratic equation has no real numbered roots."))
  }
  else if(discriminant > 0) {
    x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
    x_int_neg <- (-b - sqrt(discriminant)) / (2*a)

    return(paste0("The two x-intercepts for the quadratic equation are ",
                  format(round(x_int_plus, 5)), " and ",
                  format(round(x_int_neg, 5)), "."))
  }
  else #discriminant = 0  case
    x_int <- (-b) / (2*a)
    return(paste0("The quadratic equation has only one root. This root is ",
                  x_int))
}

quadraticRoots(1, 0, 5)
## [1] "You have chosen the quadratic equation 1x^2 + 0x + 5."
## [1] "This quadratic equation has no real numbered roots."

Creating The Quadratic Formula Function in R

quadraticRoots <- function(a, b, c) {

  print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))

  discriminant <- (b^2) - (4*a*c)

  if(discriminant < 0) {
    return(paste0("This quadratic equation has no real numbered roots."))
  }
  else if(discriminant > 0) {
    x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
    x_int_neg <- (-b - sqrt(discriminant)) / (2*a)

    return(paste0("The two x-intercepts for the quadratic equation are ",
                  format(round(x_int_plus, 5)), " and ",
                  format(round(x_int_neg, 5)), "."))
  }
  else #discriminant = 0  case
    x_int <- (-b) / (2*a)
    return(paste0("The quadratic equation has only one root. This root is ",
                  x_int))
}
quadraticRoots(1, 7, 5)
## [1] "You have chosen the quadratic equation 1x^2 + 7x + 5."
## [1] "The two x-intercepts for the quadratic equation are -0.80742 and -6.19258."

Creating The Quadratic Formula Function in R

quadraticRoots <- function(a, b, c) {

  print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))

  discriminant <- (b^2) - (4*a*c)

  if(discriminant < 0) {
    return(paste0("This quadratic equation has no real numbered roots."))
  }
  else if(discriminant > 0) {
    x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
    x_int_neg <- (-b - sqrt(discriminant)) / (2*a)

    return(paste0("The two x-intercepts for the quadratic equation are ",
                  format(round(x_int_plus, 5)), " and ",
                  format(round(x_int_neg, 5)), "."))
  }
  else #discriminant = 0  case
    x_int <- (-b) / (2*a)
    return(paste0("The quadratic equation has only one root. This root is ",
                  x_int))
}
quadraticRoots(2, 4, 2)
## [1] "You have chosen the quadratic equation 2x^2 + 4x + 2."
## [1] "The quadratic equation has only one root. This root is -1"

Creating The Quadratic Formula Function in R

quadraticRoots <- function(a, b, c = 1) {

  print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))

  discriminant <- (b^2) - (4*a*c)

  if(discriminant < 0) {
    return(paste0("This quadratic equation has no real numbered roots."))
  }
  else if(discriminant > 0) {
    x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
    x_int_neg <- (-b - sqrt(discriminant)) / (2*a)

    return(paste0("The two x-intercepts for the quadratic equation are ",
                  format(round(x_int_plus, 5)), " and ",
                  format(round(x_int_neg, 5)), "."))
  }
  else #discriminant = 0  case
    x_int <- (-b) / (2*a)
    return(paste0("The quadratic equation has only one root. This root is ",
                  x_int))
}
quadraticRoots(2, 4, 2)
## [1] "You have chosen the quadratic equation 2x^2 + 4x + 2."
## [1] "The quadratic equation has only one root. This root is -1"

Creating The Quadratic Formula Function in R

quadraticRoots <- function(a, b, c = 1) {

  print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))

  discriminant <- (b^2) - (4*a*c)

  if(discriminant < 0) {
    return(paste0("This quadratic equation has no real numbered roots."))
  }
  else if(discriminant > 0) {
    x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
    x_int_neg <- (-b - sqrt(discriminant)) / (2*a)

    return(paste0("The two x-intercepts for the quadratic equation are ",
                  format(round(x_int_plus, 5)), " and ",
                  format(round(x_int_neg, 5)), "."))
  }
  else #discriminant = 0  case
    x_int <- (-b) / (2*a)
    return(paste0("The quadratic equation has only one root. This root is ",
                  x_int))
}
quadraticRoots(2, 4)
## [1] "You have chosen the quadratic equation 2x^2 + 4x + 1."
## [1] "The two x-intercepts for the quadratic equation are -0.29289 and -1.70711."