*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
Find the standard deviation of all the columns of the following matrix:
Find the \(25th\), \(50th\), and \(75th\) quartiles for each row of the following matrix:
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
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
Using the vector below, compute the length of each string using sapply
and str_length()
from tidyverse
Using the list generated below, compute the mean of each element of the list using sapply
.
Using the vector below, use the sapply()
to find \(\log(x)\) for each value and return a matrix:
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
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.
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
Using the penguins
data set from the palmerpenguins
package, compute the average bill_length_mm
for each island
.
The vectors below provide the heights of different trees in the sample. Compute the median for each type of tree.