1 Functions

  1. Define a function to transform temperature from Fahrenheit to Celsius \[ Fahrenheit = Celsius *1.8 + 32 \]

    See solution

    fahr2cel <- function(x){
      res <- (x - 32) / 1.8
    
      return(res)
    }
    
    fahr2cel(68)
    ## [1] 20
  2. Define a function n_and_media() that given a numeric vector returns the number of elements and the vector mean in a nicely formatted sentence > The length is...
    The mean is ...

    See solution

    n_and_media <- function(x){
      x_length <- length(x)
      x_mean <- mean(x)
    
      cat("The length is", x_length,
          "\nThe mean is", round(x_mean, 3))
    }
    
    n_and_media(1:10)
    ## The length is 10 
    ## The mean is 5.5
  3. Define a function that compute the product between two values. Values need to be interactively defined by the user using readline().

    See solution

    my_product <- function(){
      x <- readline(prompt = "First value: ")
      y <- readline(prompt = "Second value: ")
    
      res <- as.numeric(x) * as.numeric(y)
    
      return(res)
    }

2 Conditional Programming

  1. Create a function that indicate whether a number is even or odd.

    See solution

    even_odd <- function(x){
      if(x %% 2 == 0){
        cat(x, "is even")
      } else {
        cat(x, "is odd")
      }
    }
    
    even_odd(4)
    ## 4 is even
    even_odd(5)
    ## 5 is odd
  2. Define a function that assign a mark according to percentage of correct answers > F \(<\) .55
    > .55 \(<=\) E \(<\) .65
    > .65 \(<=\) D \(<\) .75
    > .75 \(<=\) C \(<\) .85
    > .85 \(<=\) B \(<\) .95
    > .95 \(<=\) A

    See solution

    test_mark <- function(x){
      if(x < .55){
        res <- "F"
      } else if (x < .65) {
        res <- "E"
      } else if (x < .75) {
        res <- "D"
      } else if (x < .85) {
        res <- "C"
      } else if (x < .95) {
        res <- "B"
      } else {
        res <- "A"
      }
    
      return(res)
    }
    
    test_mark(.3)
    ## [1] "F"
    test_mark(.7)
    ## [1] "D"
    test_mark(.9)
    ## [1] "B"

3 Iterative Programming

  1. Crete a function that computes the mean of a numeric vector.
    See solution
    my_mean <- function(x){
      x_length <- length(x)
    
      # initialize values
      x_sum <- 0
    
      # for loop
      for(i in seq_along(x)){
        x_sum <-  x_sum + x[i]
      }
    
      res <- x_sum/x_length
    
      return(res)
    }
    
    my_mean(c(4, 2, -3, 6, 0))
    ## [1] 1.8
    mean(c(4, 2, -3, 6, 0))
    ## [1] 1.8
  2. Crete a function that returns the min and the max of a numeric vector.
    See solution
    my_range <- function(x){
      x_length <- length(x)
    
      # initialize values
      x_min <- x[1]
      x_max <- x[1]
    
      # for loop
      for(i in seq_along(x)[-1]){
    
        # check min
        if(x[i] < x_min){
          x_min <- x[i]
        }
    
        # check max
        if(x[i] > x_max){
          x_max <- x[i]
        }
      }
    
      res <- c(x_min, x_max)
    
      return(res)
    }
    
    my_range(c(4, 2, -3, 6, 0))
    ## [1] -3  6
    range(c(4, 2, -3, 6, 0))
    ## [1] -3  6
  3. Crete a function that computes the median of a numeric vector.
    See solution
    my_median <- function(x){
      x_length <- length(x)
    
      x <- sort(x)
    
      if(x_length %% 2 == 1){
        res <- x[x_length %/% 2 + 1]
      } else {
        res <- mean(c(x[x_length %/% 2], x[x_length %/% 2 + 1]))
      }
    
      return(res)
    }
    
    my_median(x = c(4, 2, 6, 0))
    ## [1] 3
    median(c(4, 2, 6, 0))    
    ## [1] 3