Introduction

When should we write a function

# Create a data frame
df <- tibble::tibble(
  a = rnorm(10),
  b = rnorm(10),
  c = rnorm(10),
  d = rnorm(10)
)
# Rescale each column
df$a <- (df$a - min(df$a, na.rm = TRUE)) / 
  (max(df$a, na.rm = TRUE) - min(df$a, na.rm = TRUE))
df$b <- (df$b - min(df$b, na.rm = TRUE)) / 
  (max(df$b, na.rm = TRUE) - min(df$a, na.rm = TRUE))
df$c <- (df$c - min(df$c, na.rm = TRUE)) / 
  (max(df$c, na.rm = TRUE) - min(df$c, na.rm = TRUE))
df$d <- (df$d - min(df$d, na.rm = TRUE)) / 
  (max(df$d, na.rm = TRUE) - min(df$d, na.rm = TRUE))
Rescale <- function(x) {
    
    # body
    
    x <- (x - min(x, na.rm = TRUE)) / 
  (max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
# return value
return (x)
}
df$a <- Rescale(df$a)
df$b <- Rescale(df$b)
df$c <- Rescale(df$c)
df$d <- Rescale(df$d)

df
## # A tibble: 10 × 4
##           a     b      c     d
##       <dbl> <dbl>  <dbl> <dbl>
##  1 0.232    0.823 0.269  0.523
##  2 0.000357 0.499 0.731  0.552
##  3 0.0711   1     0.159  0.430
##  4 0.737    0.694 1      0.849
##  5 0.469    0.685 0.0850 0.403
##  6 0.468    0.782 0      0.267
##  7 0.0696   0.794 0.127  0.652
##  8 0        0.715 0.218  0    
##  9 1        0.534 0.523  0.539
## 10 0.757    0     0.871  1

When creating functions and name, it is better that the prefix of the names are the same and the last part after the _ is what differs.

Conditional Execution

|| = or && = and

detect_sign <- function(x) {
    
    if(x>0) {
        message("Value is positive")
        print(x)
    } else if(x == 0) {
        warning("Value is not positive, but it can be accepted")
        print(x)
    } else{
        stop("Value is negative, the function must stop")
        print(x)
}
}

3 %>% detect_sign
## Value is positive
## [1] 3
0 %>% detect_sign
## Warning in detect_sign(.): Value is not positive, but it can be accepted
## [1] 0
# -1 %>% detect_sign

Function Arguments

x <- c(1:10, 100, NA)
x
##  [1]   1   2   3   4   5   6   7   8   9  10 100  NA
x %>% mean()
## [1] NA
x %>% mean(na.rm = TRUE)
## [1] 14.09091
x %>% mean(na.rm = TRUE, trim = 0.1)
## [1] 6
mean_remove_na <- function(x, na.rm = TRUE, ...) {
    
    avg <- mean(x, na.rm = na.rm, ...)
    
    return(avg)
}

x %>% mean_remove_na()
## [1] 14.09091
x %>% mean_remove_na(na.rm = FALSE)
## [1] NA
x %>% mean_remove_na(trim = 0.1)
## [1] 6

Return Values