The apply() function – a useful function in R when working with rows and columns of data frames or matrixes. Today, we will give you a completed tutorial on apply() function in R with various useful examples.
Let’s move on to discover all about the function.
What is the apply() function in R?
The apply()
function is practical for manipulating data frames or matrices. The function takes data frames and matrices as input and executes commands that directly affect the rows and columns that are detected by the parameter “margin”.
Syntax:
apply(input, margin, function)
Parameters:
- input: A data frame or matrix.
- margin: Determine which field will be applied to the function. 1 for rows, 2 for columns, and c(1,2) for both rows and columns,
- function: The function executing on the data,
How to use the apply() function?
Execute the function on rows of the input data
In this example, we will use the apply() function on rows of a matrix and pass the max function to find the maximum element in each matrix row. The result is a list that contains the maximum element in each row.
Code:
# Create a random vector having 12 elements myVector <- c(sample(0:30, 12)) # Create a 3x4 matrix from the vector myMatrix <- matrix(myVector, nrow = 3, ncol = 4) print("Our 3x4 matrix is:") print(myMatrix) # Find the max element in each row of the matrix # 1 refers to rows of the matrix max_row_element = apply(myMatrix, 1, max) print("The maximum elements in each row are:") print(max_row_element)
Result:
[1] "Our 3x4 matrix is:"
[,1] [,2] [,3] [,4]
[1,] 30 14 6 8
[2,] 2 7 9 22
[3,] 27 13 0 23
[1] "The maximum elements in each row are:"
[1] 30 22 27
Execute the function on columns of the input data
Now, we will see how to use the apply() function on columns of a matrix. In this example, we will calculate the mean of all values in each column of a matrix:
- We will create a random matrix like the first example.
- We use a margin’s value equal to 2 to tell the function to apply on input data columns.
- We use the mean function to find the mean value in each column.
Code:
# Create a random vector having 12 elements myVector <- c(sample(0:30, 12)) # Create a 3x4 matrix from the vector myMatrix <- matrix(myVector, nrow = 3, ncol = 4) print("Our 3x4 matrix is:") print(myMatrix) # Find the mean values in each column of the matrix # 2 refers to the columns of the matrix mean_column_value = apply(myMatrix, 2, mean) print("The mean values in each row are:") print(mean_column_value)
Result:
[1] "Our 3x4 matrix is:"
[,1] [,2] [,3] [,4]
[1,] 23 4 0 14
[2,] 13 19 11 30
[3,] 15 6 1 3
[1] "The mean values in each row are:"
[1] 17.000000 9.666667 4.000000 15.666667
Execute the function on both rows and columns of the input
Note that applying the function to both rows and columns is actually to apply the function to each value of itself. Take a look at 2 following examples to see the difference.
Example 1: Apply the sum function on rows and columns
# Create a random vector having 12 elements myVector <- c(1:12) # Create a 3x4 matrix from the vector myMatrix <- matrix(myVector, nrow = 3, ncol = 4) print("Our 3x4 matrix is:") print(myMatrix) # Apply the sum function on both rows and columns sumMatrix = apply(myMatrix, c(1, 2), sum) print("The matrix after applying the sum function are:") print(sumMatrix)
Result:
[1] "Our 3x4 matrix is:"
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[1] "The matrix after applying the sum function are:"
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
Example 2: Apply a custom function on rows and columns
# Create a random vector having 12 elements myVector <- c(1:12) # Create a 3x4 matrix from the vector myMatrix <- matrix(myVector, nrow = 3, ncol = 4) print("Our 3x4 matrix is:") print(myMatrix) # A custom function to add 5 add <- function(x){ x + 5 } # Apply the custom function customMatrix = apply(myMatrix, c(1, 2), add) print("The matrix after applying the custom function are:") print(customMatrix)
Result:
[1] "Our 3x4 matrix is:"
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[1] "The matrix after applying the custom function are:"
[,1] [,2] [,3] [,4]
[1,] 6 9 12 15
[2,] 7 10 13 16
[3,] 8 11 14 17
As you can see, in the first example, the new matrix is equal to the original matrix, while in the second example, the new matrix is different from the original matrix. Now you are clear that when applying functions on both rows and columns, it applies functions to each element itself.
Summary
In summary, the apply() function in R executes a function to rows or columns of a matrix or data frame. In case executing the function on both rows and columns, it executes the function on each element itself.
Maybe you are interested:

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP