Exercises 4 - Basic Functions Solutions

Files Corresponding to Short Course Programming in R

Exercises 4 - Basic Functions Solutions

  1. Write your own absolute value function called myAbsoluteValue. There are two options for what to do with the input:

Allow the function to take in an entire vector of values by using if_else() to do your comparison.

myAbsoluteValue <- function(vector) {
    return(dplyr::if_else(vector >= 0, vector, -vector))
}
  1. Check your function works by plugging in the values below.
3
-5
c(3, -5)
-10:10
myAbsoluteValue(3)
## [1] 3
myAbsoluteValue(-5)
## [1] 5
myAbsoluteValue(c(3, -5))
## [1] 3 5
myAbsoluteValue(-10:10)
##  [1] 10  9  8  7  6  5  4  3  2  1  0  1  2  3  4  5  6  7  8  9 10