Chapter 7 R and RStudio - You Should Know

7.1 R Project

Use R Project for manage your specific workspace. It will save your code, data, and history.

7.2 Apply Family for Iteration

In R, the apply family of functions (including apply, lapply, sapply, tapply, and others) are commonly used for iteration because they provide a concise and efficient way to apply a function to elements of a data structure, such as a matrix, data frame, list, or vector.

Some advantages of using apply family of functions :

  1. Concise syntax
  2. Flexibility : flexible in term of input and output used. We will discuss it on the details of each function.
  3. Efficiency : The apply family functions are optimized for performance, often outperforming explicit loops in terms of speed and memory usage, especially for large datasets.

Details of each functions :

7.2.1 apply(X, MARGIN, FUN)

  • Input: data frame or matrix
  • Output: vector
  • Parameters used:
    • X: data frame or matrix
    • MARGIN: a value of 1 or 2 that defines how the function will be executed
      • 1: computation is performed on rows
      • 2: computation is performed on columns
    • FUN: the function applied to elements of X

Here’s an example code using apply():

# Define the data will be used
(m <- matrix(c(1:16), 4))
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16
# Compute mean across rows
apply(m, MARGIN = 1, FUN = mean)
## [1]  7  8  9 10
# Using apply() with a custom built-in function
apply(m, 1, function(x){max(x) - min(x)})
## [1] 12 12 12 12

7.2.2 lapply(X, FUN)

  • Input: list or dataframe
  • Output: list with the same length as input list / number of columns in the dataframe input
  • Parameters used:
    • X: data frame or list
    • FUN: the function applied to elements of X

Here’s an example code using lapply():

# Define the data will be used
(m <- list(a=c(1:16),b=c(2:17), c=c(3:18)))
## $a
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
## 
## $b
##  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17
## 
## $c
##  [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
# Compute mean
lapply(m, FUN = mean)
## $a
## [1] 8.5
## 
## $b
## [1] 9.5
## 
## $c
## [1] 10.5
# Using lapply() with a custom built-in function
lapply(m, function(x){max(x) - min(x)})
## $a
## [1] 15
## 
## $b
## [1] 15
## 
## $c
## [1] 15

7.2.3 sapply(X, FUN)

  • Input: list or dataframe
  • Output: vector with the same length as input list / number of columns in the dataframe input
  • Parameters used:
    • X: data frame or list
    • FUN: the function applied to elements of X

Here’s an example code using sapply():

# Define the data will be used
(m <- list(a=c(1:16),b=c(2:17), c=c(3:18)))
## $a
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
## 
## $b
##  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17
## 
## $c
##  [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
# Compute mean
sapply(m, FUN = mean)
##    a    b    c 
##  8.5  9.5 10.5
# Using lapply() with a custom built-in function
sapply(m, function(x){max(x) - min(x)})
##  a  b  c 
## 15 15 15

7.2.4 tapply(X, INDEX, FUN)

  • Input: vector and factor/category used in the calculation
  • Output: vector with the length of category
  • Parameters used:
    • X: data frame or list
    • INDEX: factor / category
    • FUN: the function applied to elements of X

Here’s an example code using tapply():

# Define the data will be used
(m <- data.frame(category = c(rep("a",6),rep("b",10)), x=c(1:16)))
##    category  x
## 1         a  1
## 2         a  2
## 3         a  3
## 4         a  4
## 5         a  5
## 6         a  6
## 7         b  7
## 8         b  8
## 9         b  9
## 10        b 10
## 11        b 11
## 12        b 12
## 13        b 13
## 14        b 14
## 15        b 15
## 16        b 16
# Compute mean
tapply(m$x, m$category, mean)
##    a    b 
##  3.5 11.5
# Using lapply() with a custom built-in function
tapply(m$x, m$category, function(x){max(x) - min(x)})
## a b 
## 5 9

7.2.5 mapply(FUN, PARAMETER)

  • Note: multivariate form of sapply()
  • Parameters used:
    • FUN: the function applied to elements of X
    • PARAMETER: parameter used in the function

Here’s an example code using mapply():

## replicating using the function rep with parameters 1:5 and 5:1
mapply(rep, 1:5, 5:1)
## [[1]]
## [1] 1 1 1 1 1
## 
## [[2]]
## [1] 2 2 2 2
## 
## [[3]]
## [1] 3 3 3
## 
## [[4]]
## [1] 4 4
## 
## [[5]]
## [1] 5
## generating normal data with parameters n=10, mean=1:10 
## there will be 10 vectors of generated data, with mean =  1,2,3, .. 10, and deviation standard = 1
mapply(rnorm, 10, 1:10, 1)
##              [,1]      [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
##  [1,]  0.44797649 2.0796352 4.147248 2.766707 4.316956 5.763322 5.360536
##  [2,] -0.24665977 1.5280371 2.698825 4.859623 4.026350 5.859897 7.203274
##  [3,]  2.60515883 4.7930627 2.396542 3.054079 3.842293 5.215888 5.041016
##  [4,]  1.66251080 0.3618711 2.338592 3.134407 6.284778 5.270667 5.754582
##  [5,]  2.47244163 2.3638376 4.316376 4.141314 6.127771 7.408838 5.237441
##  [6,]  1.94193920 2.0826607 3.266243 4.493398 3.458544 5.524035 7.191797
##  [7,]  1.09296994 2.0806067 4.091427 4.196791 7.030884 5.059257 7.406666
##  [8,] -0.04096255 2.8462468 2.690526 3.501306 3.773111 6.192323 7.371582
##  [9,]  3.49973585 2.6600829 2.540409 3.867390 4.924744 5.069554 5.602310
## [10,]  1.32269059 2.9486165 2.058852 2.803086 4.324929 6.597945 5.722114
##            [,8]     [,9]     [,10]
##  [1,]  7.463644 8.989514  9.807215
##  [2,]  8.451889 8.301317 10.304048
##  [3,] 10.357538 7.993509 10.508392
##  [4,]  6.335257 9.485623 11.136897
##  [5,]  6.282352 8.180883  9.707303
##  [6,]  9.381808 8.095239 10.034798
##  [7,]  8.301836 8.845208 10.532305
##  [8,]  8.211196 8.056411 11.437152
##  [9,]  8.537863 9.809711 10.652724
## [10,]  7.483068 8.024514 11.349001