** rapply()** function in R Language is used to recursively apply a function to a list.

Syntax:

rapply(object, f, classes = “ANY”, deflt = NULL, how = c(“unlist”, “replace”, “list”))

Parameters:

object:represents list or an expression

f:represents function to be applied recursively

classes:represents class name of the vector or “ANY” to match any of the class

deflt:represents default result when how is not “replace”

how:represents modes

The modes in ** rapply()** function are of 2 basic types. If

**how = “replace”**, each element of the list object which is not itself is a list and has a class included in classes then each element of the list is replaced by the resulting value of the function

**f**applied to the element.

If **how = “list”** or **how = “unlist”**, list object is copied and all non-list elements which are included in classes are replaced by the resulting value of the function **f** applied to the element and all other are replaced by **deflt**.

**Example 1: Using replace mode**

`# Defining a list ` `ls <- ` `list` `(a = 1:5, b = 100:110, c = ` `c` `(` `'a'` `, ` `'b'` `, ` `'c'` `)) ` ` ` `# Print whole list ` `cat` `(` `"Whole List: \n"` `) ` `print` `(ls) ` ` ` `# Using replace mode ` `cat` `(` `"Using replace mode:\n"` `) ` `rapply` `(ls, mean, how = ` `"replace"` `, classes = ` `"integer"` `) ` |

*chevron_right*

*filter_none*

**Output:**

Whole List: $a [1] 1 2 3 4 5 $b [1] 100 101 102 103 104 105 106 107 108 109 110 $c [1] "a" "b" "c" Using replace mode: $a [1] 3 $b [1] 105 $c [1] "a" "b" "c"

**Example 2: Using list mode**

`# Defining a list ` `ls <- ` `list` `(a = 1:5, b = 100:110, c = ` `c` `(` `'a'` `, ` `'b'` `, ` `'c'` `)) ` ` ` `# Print whole list ` `cat` `(` `"Whole List: \n"` `) ` `print` `(ls) ` ` ` `# Using list mode ` `cat` `(` `"Using list mode:\n"` `) ` `rapply` `(ls, mean, how = ` `"list"` `, classes = ` `"integer"` `) ` |

*chevron_right*

*filter_none*

**Output:**

Whole List: $a [1] 1 2 3 4 5 $b [1] 100 101 102 103 104 105 106 107 108 109 110 $c [1] "a" "b" "c" Using list mode: $a [1] 3 $b [1] 105 $c NULL

**Example 3: Using unlist mode**

`# Defining a list ` `ls <- ` `list` `(a = 1:5, b = 100:110, c = ` `c` `(` `'a'` `, ` `'b'` `, ` `'c'` `)) ` ` ` `# Print whole list ` `cat` `(` `"Whole List: \n"` `) ` `print` `(ls) ` ` ` `# Using unlist mode ` `cat` `(` `"Using unlist mode:\n"` `) ` `rapply` `(ls, mean, how = ` `"unlist"` `, classes = ` `"integer"` `) ` |

*chevron_right*

*filter_none*

**Output:**

Whole List: $a [1] 1 2 3 4 5 $b [1] 100 101 102 103 104 105 106 107 108 109 110 $c [1] "a" "b" "c" Using unlist mode: a b 3 105

## Recommended Posts:

- Apply a Function over a List of elements in R Programming - lapply() Function
- Apply a Function over a Ragged Array in R Programming - tapply() Function
- Perform Operations over Margins of an Array or Matrix in R Programming - apply() Function
- Convert an Object to List in R Programming - as.list() Function
- Check if the Object is a List in R Programming - is.list() Function
- Get the List of Arguments of a Function in R Programming - args() Function
- Get a List of all the Attached Packages in R Programming - search() Function
- List all the Objects present in the Current Working Directory in R Programming - ls() Function
- Coercing an Object of mode "list" to mode "call" in R Programming - as.call() Function
- Get a List of Numbers in the Specified Range in R Programming - seq.int() Function
- Get a List of all the 657 colors in R Programming - colors() Function
- Get a List of points obtained by Interpolation in R Programming - spline() and splinefun() Function
- Plotting Graphs using Two Dimensional List in R Programming
- Two Dimensional List in R Programming
- Named List in R Programming
- Check if a Function is a Primitive Function in R Programming - is.primitive() Function
- Compute Density of the Distribution Function in R Programming - dunif() Function
- Search the Interval for Minimum and Maximum of the Function in R Programming - optimize() Function
- Compute the Natural Logarithm of the Absolute Value of Gamma Function in R Programming - lgamma() Function
- Compute the Logarithmic Derivative of the gamma Function in R Programming - digamma() Function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.