*apply Functions

Re Install

  • csucistats
install.packages('csucistats', 
  repos = c('https://inqs909.r-universe.dev', 
  'https://cloud.r-project.org'))

Learning Objectives

  • *apply()
  • apply()
  • lapply()
  • sapply
  • mapply()
  • tapply()

*apply()

*apply()

The *apply() functions are a set of functions that completes iterative tasks to each element of an R object.

apply()

apply()

The apply function returns a vector, array, or list of values by applying a function to the margins of an array. You will need to specify the following arguments:

  • X: an array to be indexed and applied

  • MARGIN: specifyng which index(es) to subset by

  • FUN: function to be applied

  • : further arguments to be applied to FUN, must be labeled

apply(X, MARGIN, FUN, ...)

Example

Find the standard deviation of all the columns of the following matrix:

x <- matrix(rnorm(1000), nrow = 10)

Example

Find the \(25th\), \(50th\), and \(75th\) quartiles for each row of the following matrix:

x <- matrix(rnorm(1000), nrow = 20)

lapply()

lapply()

The lapply function applies a function to all the elements of a vector or matrix, and it will return a list. You will need to specify the following arguments:

  • X: object to be iterated

  • FUN: a function to be applied

  • : further arguments to be passed along to FUN

lapply(X, FUN, ...)

Example

Create a function that returns a labeled list for with the following values: mean, standard deviation, median, max, and min.

sapply()

sapply()

The sapply() function will apply a function to each element of a list or vector, and it will return a simplified object, vector, matrix, or array. The sapply() function uses 4 main arguments:

  • X: a vector or list to be iterated

  • FUN: a function to be applied

  • : arguments passed along to FUN, must be labeled

  • simplify: indicates how to simplify the function, defaults to n-dimensional array based on output

sapply(X, FUN, ..., simplify = TRUE)

Example

Using the vector below, compute the length of each string using sapply and str_length() from tidyverse

fruits <- c("apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon")

Example

Using the list generated below, compute the mean of each element of the list using sapply.

# Generate a list of 10 lists, each containing 5 random numbers
lists <- lapply(1:10, function(i) {
  means <- rpois(1, 3)
  rnorm(5, means)
})

Example

Using the vector below, use the sapply() to find \(\log(x)\) for each value and return a matrix:

numbers <- 4:400

mapply()

mapply()

The mapply() is the multivariate function of sapply(). The mapply() function has 3 major arguments:

  • FUN: function applied to data

  • : arguments to be iterated, must be labeled.

  • MoreArgs: A list containing other arguments that are necessary to FUN

mapply(FUN, ..., MoreArgs = NULL)

Example

Let x and y be two vectors, shown below, represent the x and y coordinates of a point. Using mapply(), compute the the distance between the points and the origin.

x <- c(2, 3, 4, 5)
y <- c(4, 6, 8, 10)

tapply()

tapply()

The tapply() function will apply function to a group of values based on and indexed lists. It takes 3 arguments:

  • X: a vector that can split

  • INDEX: the index list made up of factors

  • FUN: the function that will be applied

  • : further arguments to be passed to FUN

tapply(X, INDEX, FUN = NULL, ...)

Example

Using the penguins data set from the palmerpenguins package, compute the average bill_length_mm for each island.

Example

The vectors below provide the heights of different trees in the sample. Compute the median for each type of tree.

heights <- c(70, 72, 68, 65, 80, 75, 60, 68, 90, 72)
species <- c("maple", "oak", "pine", "maple", "oak", "pine", "maple", "oak", "pine", "maple")