Javatpoint Logo
Javatpoint Logo

Apply(), lapply(), sapply(), tapply() Function in R with Examples

apply() Function in R:

Applying a function to a matrix, array, or list is done in R using the apply() function. It is an extremely helpful function for doing out actions on the data kept in these structures.

The following is the apply() function's syntax:

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

Here;

  • The margin that the function should be applied to is specified by the MARGIN parameter. It can be a vector of these values, such as 1, 2, or both for rows and columns.
  • X is the matrix, array, or list being operated on.
  • FUN is the desired outcome.
  • The function's optional arguments are contained in "... ".

Here are a few examples of apply():

Example 1: Applying a Function to a Matrix by Rows

Let's say we have the matrix shown below:

Apply() can be used to determine the mean of each row:

Output:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

Example 2: Applying a Function to a Matrix by Columns

Apply() can be used to determine the standard deviation for each column of a matrix m:

Output:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

Example 3: Applying a Function to a List

Consider a list of vectors:

Apply() can be used to determine the sum of each vector:

Output:

The sum of each vector in the list will be reported in this way:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

Note: Due to the fact that we want to apply the function to each vector in the list, we use 1 as the value of MARGIN in this instance (i.e., along the first margin).

Example 4: Applying a User-Defined Function to a Matrix by Rows

Let's say we have the matrix shown below:

Apply() allows us to apply a user-defined function to each row:

Output:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

The f() function computes the sum of the squares for each row in this example.

All things considered, apply() is a fairly strong function that can be applied in a number of different ways to manipulate matrices, arrays, and lists.

lapply() Function in R:

A useful feature of the R programming language is the lapply() function, which enables you to apply a specific function to each element in a list or vector. A list with the same length as the input is produced as the output, with each entry representing the outcome of applying the specified function to its corresponding input element.

The lapply() function's underlying syntax is as follows:

In this case, FUN is the function that will be applied to each member of X. X is the input list or vector. You can add more parameters to the FUN function by passing them as the "... argument."

Some Examples of lapply():

Example 1:

Let's look at an illustration of how to employ R's lapply() function.

Let's say we want to determine the square root of each number in a list of numbers. Each element of the list can have the sqrt() function applied to it using the lapply() method. Here is the key:

Code:

Output:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

As you can see, the sqrt() function was applied to each element of the list by the lapply() function, which then returned a list that had the same length as the input and contained elements that were the square roots of the corresponding input elements.

Example 2:

The lapply() function can also be used with user-defined functions. Let's make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:

Code:

Output:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

In this instance, the add 10() function was applied to each element of the list by the lapply() function, which resulted in a list that had the same length as the input and contained each element as the result of adding 10 to its corresponding input element.

It's significant to remember that, regardless of the input, the lapply() method always produces a list. For instance, the result of using the lapply() method on a vector is still a list: Here is an example to show this.

Code:

Output:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

In this instance, the sqrt() function was used after the lapply() method transformed the input vector into a list.

sapply() Function in R:

A helpful feature of the R programming language is the sapply() function, which may be used to streamline the code for applying a specified function to each element of a list or vector. A vector or matrix with the same length or dimensions as the input is produced as the output, with each element the outcome of applying the specified function to its corresponding input element.

The sapply() function's syntax is as follows:

In this case, FUN is the function that will be applied to each member of X. X is the input list or vector. By default, the simplified parameter is set to TRUE, meaning that if the function's output is a vector or matrix, the result will also be a vector or matrix. To send more arguments to the FUN function, use the "... argument."

Let's look at an illustration of how to employ R's sapply() function.

Let's say we want to determine the square root of each number in a list of numbers. The sqrt() function can be applied to each element of the list using the sapply() method. Here is the key:

Code:

Output:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

As you can see, the sapply() method took each element of the list and applied the sqrt() function to it. It then returned a vector with the same length as the input, each element of which was the square root of its corresponding input element.

The sapply() function can also be used with user-defined functions. Let's make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:

Code:

Output:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

In this instance, the sapply() function added 10 to each element of the list using the add 10() function, returning a vector with the same length as the input where each element was the result of the addition of 10.

It's crucial to remember that the sapply() function also works with vectors. In this situation, a vector will still be the result:

Code:

Output:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

The sapply() method in this situation treated the input vector as a list and applied the sqrt() function to each element, returning a vector with the same length as the input and each element being the square root of its corresponding input element.

tapply() Function in R:

For applying a specified function to subsets of a vector or array based on the values of another variable, the R language's tapply() function is a helpful tool. Depending on the function used, the output is a vector, array, or list.

The tapply() function's fundamental syntax is as follows:

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

Here, FUN is the function to be applied to each subset, X is the input vector or array, INDEX is a factor or list of factors denoting the subsets, and... are optional parameters that can be supplied to the function FUN.

Let's look at an illustration of how to employ R's tapply() function.

Assume we have a vector of categories and a vector of numbers. The average of the values for each category is what we are looking for.

The mean() function can be applied to each subset of the vector based on the category using the tapply() function. The code is here;

Code:

Output:

Apply(), lapply(), sapply(), tapply() Function in R with Examples

As you can see, the tapply() function used the mean() function to analyze each subset of the vector according to the category, returning a named vector with the results for each category.

Conclusion:

Simplified vectors or matrices with the same lengths or dimensions as the input can be obtained by applying a given function to each element of a list or vector using R's sapply() function.

Developing code can save a lot of time and effort, especially when working with large datasets. Moreover, the sapply() method is a flexible function that may be used with lists, vectors, and built-in and user-defined functions.

Everyone working with R and dealing with data processing and analysis should be familiar with this function.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA