apply() in R

What is the apply() Function?

The apply() function in R proves handy for executing a function on the components of a data structure like a matrix or a data frame. It simplifies handling datasets by enabling the repetition of tasks across multiple elements efficiently. To use this function you need to provide three inputs; the data structure, the margin (indicating whether to apply the function row wise or column wise) and the desired function. This feature streamlines data processing tasks and minimizes the requirement, for writing code.

Why is apply() Important in R Programming?

The apply() function plays a role in R programming as it comes with numerous benefits compared to conventional loops. It speeds up the process by using internal coding making it great, for managing extensive datasets. Moreover apply() streamlines the code structure resulting in a compact and understandable format that lowers the chances of mistakes and enhances code manageability.

Basic Syntax of apply()

The basic syntax of the apply() function includes three parameters:

  • X: The array or matrix on which the function will be applied.
  • MARGIN: Specifies whether the function should be applied across rows (1) or columns (2).
  • FUN: The function to be applied, which can be a built-in or user-defined function.

Different Types of apply() Functions in R

lapply()

The lapply() function is used to execute a given function on every item in a list or data frame producing a list with the same number of elements as the original input. It comes in handy for looping through objects, within a collection.

sapply()

Sapply() works like lapply() but it streamlines the result into an array or matrix whenever feasible. It proves useful when aiming for an output layout.

vapply()

Vapply() works similarly to sapply(). It mandates specifying the anticipated output type through the FUN.VALUE parameter. This additional aspect guarantees uniformity in the output format enhancing the functions reliability, for data analysis.

Applying Functions to Different Data Structures

Applying Functions to Vectors

When you use a function on a vector you have the flexibility to work with each element separately like how you work with rows or columns in a matrix. This opens up possibilities, for performing different operations and changes.

Applying Functions to Matrices

The apply() function allows you to apply a function to either the rows or columns of a matrix. You can choose whether the function should be applied to rows by setting the MARGIN argument to 1 or, to columns by setting it to 2.

Applying Functions to Data Frames

The apply() function also works with data frames so you can carry out operations on rows or columns. For instance you can find the average of each column in a data frame by using apply(df, 2 mean).

Customizing Functions with Extra Arguments

To pass extra arguments to a function applied using apply(), simply include them after the function name. For example, apply(my_data, 1, my_function, my_argument) will apply my_function to each row of my_data while passing my_argument as an additional argument. This flexibility allows for more customized and versatile data processing.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate