Loop Functions
7.1 Looping on the Command Line
Writing for and while loops is useful when programming but not particularly easy when working
interactively on the command line. Multi-line expressions with curly braces are just not that easy to
sort through when working on the command line. R has some functions which implement looping
in a compact form to make your life easier.
lapply(): Loop over a list and evaluate a function on each element
sapply(): Same as lapply but try to simplify the result
apply(): Apply a function over the margins of an array
tapply(): Apply a function over subsets of a vector
mapply(): Multivariate version of lapply
An auxiliary function split is also useful, particularly in conjunction with lapply.
In R, the apply() function and its variants (lapply(), sapply(), tapply(), etc.) are used to apply a function over data
structures such as vectors, matrices, and data frames. These functions help simplify loops and make your code
more concise and efficient.
1. apply()
The apply() function is used to apply a function to the rows or columns of a matrix or 2D array.
Syntax:
apply(X, MARGIN, FUN, ...)
X: The matrix or array.
MARGIN: An integer value. Use 1 to apply the function to rows and 2 to apply it to columns.
FUN: The function to apply.
Example: Sum the rows and columns of a matrix
matrix_data <- matrix(1:9, nrow = 3)
# Sum each row
row_sum <- apply(matrix_data, 1, sum)
print(row_sum)
# Output: 6 15 24 (sum of each row)
# Sum each column
col_sum <- apply(matrix_data, 2, sum)
print(col_sum)
# Output: 12 15 18 (sum of each column)
2. lapply()
The lapply() function applies a function to each element of a list or vector and returns a list.
Syntax:
lapply(X, FUN, ...)
, X: The list or vector.
FUN: The function to apply.
Example: Applying a function to a list
my_list <- list(a = 1:5, b = 6:10)
# Apply sum to each element in the list
result <- lapply(my_list, sum)
print(result)
# Output: $a 15, $b 40 (sum of each vector in the list)
3. sapply()
The sapply() function is similar to lapply(), but it simplifies the output. It attempts to return a vector or matrix
when possible.
Syntax:
sapply(X, FUN, ...)
X: The list or vector.
FUN: The function to apply.
Example: Simplifying results to a vector
my_list <- list(a = 1:5, b = 6:10)
# Apply sum to each element and simplify the result
result <- sapply(my_list, sum)
print(result)
# Output: a 15, b 40 (vector output)
4. tapply()
The tapply() function applies a function over subsets of a vector, defined by a factor (grouping variable).
Syntax:
tapply(X, INDEX, FUN, ...)
X: The vector to apply the function to.
INDEX: A factor or list of factors that define the groups.
FUN: The function to apply.
Example: Calculate the mean of values by group
data <- c(10, 20, 30, 40, 50)
group <- factor(c('A', 'A', 'B', 'B', 'A'))
# Apply mean to each group
result <- tapply(data, group, mean)
print(result)
# Output: A 26.66667, B 35 (mean for each group)
5. mapply()
7.1 Looping on the Command Line
Writing for and while loops is useful when programming but not particularly easy when working
interactively on the command line. Multi-line expressions with curly braces are just not that easy to
sort through when working on the command line. R has some functions which implement looping
in a compact form to make your life easier.
lapply(): Loop over a list and evaluate a function on each element
sapply(): Same as lapply but try to simplify the result
apply(): Apply a function over the margins of an array
tapply(): Apply a function over subsets of a vector
mapply(): Multivariate version of lapply
An auxiliary function split is also useful, particularly in conjunction with lapply.
In R, the apply() function and its variants (lapply(), sapply(), tapply(), etc.) are used to apply a function over data
structures such as vectors, matrices, and data frames. These functions help simplify loops and make your code
more concise and efficient.
1. apply()
The apply() function is used to apply a function to the rows or columns of a matrix or 2D array.
Syntax:
apply(X, MARGIN, FUN, ...)
X: The matrix or array.
MARGIN: An integer value. Use 1 to apply the function to rows and 2 to apply it to columns.
FUN: The function to apply.
Example: Sum the rows and columns of a matrix
matrix_data <- matrix(1:9, nrow = 3)
# Sum each row
row_sum <- apply(matrix_data, 1, sum)
print(row_sum)
# Output: 6 15 24 (sum of each row)
# Sum each column
col_sum <- apply(matrix_data, 2, sum)
print(col_sum)
# Output: 12 15 18 (sum of each column)
2. lapply()
The lapply() function applies a function to each element of a list or vector and returns a list.
Syntax:
lapply(X, FUN, ...)
, X: The list or vector.
FUN: The function to apply.
Example: Applying a function to a list
my_list <- list(a = 1:5, b = 6:10)
# Apply sum to each element in the list
result <- lapply(my_list, sum)
print(result)
# Output: $a 15, $b 40 (sum of each vector in the list)
3. sapply()
The sapply() function is similar to lapply(), but it simplifies the output. It attempts to return a vector or matrix
when possible.
Syntax:
sapply(X, FUN, ...)
X: The list or vector.
FUN: The function to apply.
Example: Simplifying results to a vector
my_list <- list(a = 1:5, b = 6:10)
# Apply sum to each element and simplify the result
result <- sapply(my_list, sum)
print(result)
# Output: a 15, b 40 (vector output)
4. tapply()
The tapply() function applies a function over subsets of a vector, defined by a factor (grouping variable).
Syntax:
tapply(X, INDEX, FUN, ...)
X: The vector to apply the function to.
INDEX: A factor or list of factors that define the groups.
FUN: The function to apply.
Example: Calculate the mean of values by group
data <- c(10, 20, 30, 40, 50)
group <- factor(c('A', 'A', 'B', 'B', 'A'))
# Apply mean to each group
result <- tapply(data, group, mean)
print(result)
# Output: A 26.66667, B 35 (mean for each group)
5. mapply()