rapply function – explanation and examples

In this post we will look at one of the powerful ‘apply’ group of functions in R – rapply.
rapply stands for recursive apply, and as the name suggests it is used to apply a function to all elements of a list recursively.
We begin by first creating a straightforward list

[code language=”r” gutter=”false”]
> x=list(1,2,3,4)
[/code]
we use the rapply function to get the sum of these numbers (this does not demonstrate the recursive nature, yet)
[code language=”r” gutter=”false”]
> rapply(x,function(x){x^2},class=c("numeric"))
[1] 1 4 9 16
[/code]
The first argument is the list x, the second argument is the function that needs to be applied over the list and the last argument gives the classes to which the function should be applied. so if you had a character in there, it would have been ignored
[code language=”r” gutter=”false”]
> x=list(1,2,3,4,"a")
> rapply(x,function(x){x^2},class=c("numeric"))
[1] 1 4 9 16
[/code]
Lets see the recursive nature of rapply. We begin by creating a list that contains sub lists.
[code language=”r” gutter=”false”]
> x=list(1,list(2,3),4,list(5,list(6,7)))
> str(x)
List of 4
$ : num 1
$ :List of 2
..$ : num 2
..$ : num 3
$ : num 4
$ :List of 2
..$ : num 5
..$ :List of 2
.. ..$ : num 6
.. ..$ : num 7
> rapply(x,function(x){x^2},class=c("numeric"))
[1] 1 4 9 16 25 36 49
[/code]
You would have noticed that the result is in the form of a vector, we would want to preserve the original structure.
[code language=”r” gutter=”false”]
> a=rapply(x,function(x){x^2},class=c("numeric"),how="list")
> str(a)
List of 4
$ : num 1
$ :List of 2
..$ : num 4
..$ : num 9
$ : num 16
$ :List of 2
..$ : num 25
..$ :List of 2
.. ..$ : num 36
.. ..$ : num 49
[/code]
The “how” argument can take 3 values. In the above example we use “list” to preserve the original list. What it does is it applies the function to each element of the list and sublists, if any.
If the list contains an element of a different class then that element is replaced by the element specified in the “deflt” argument
[code language=”r” gutter=”false”]
> x=list(1,list(2),"f")
> a=rapply(x,function(x){x^2},class=c("numeric"),how="list",deflt="p")
> a
[[1]]
[1] 1

[[2]]
[[2]][[1]]
[1] 4

[[3]]
[1] "p"
> a=rapply(x,function(x){x^2},class=c("numeric"),how="unlist",deflt="p")
> a
[1] "1" "4" "p"
[/code]
In the above example the function was applied to all numbers and all other elements were replaced by “p”. However, if you need the original values then use the “replace” option
[code language=”r” gutter=”false”]
> a=rapply(x,function(x){x^2},class=c("numeric"),how="replace",deflt="p")
> a
[[1]]
[1] 1

[[2]]
[[2]][[1]]
[1] 4

[[3]]
[1] "f"
[/code]
One last thing before we finish. The function inside rapply can take other arguments too. Lets see an example
[code language=”r” gutter=”false”]
> a=rapply(x,function(x,y){x^y},class=c("numeric"),how="unlist",deflt="p",y=3)
> a
[1] "1" "8" "p"
[/code]
In the above example the function takes in one more variable ‘y’ and we pass that in rapply.
See this for more tutorials in R.