**R – StudyTrails**, and kindly contributed to R-bloggers)

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

> x=list(1,2,3,4)

we use the rapply function to get the sum of these numbers (this does not demonstrate the recursive nature, yet)

> rapply(x,function(x){x^2},class=c("numeric")) [1] 1 4 9 16

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

> x=list(1,2,3,4,"a") > rapply(x,function(x){x^2},class=c("numeric")) [1] 1 4 9 16

Lets see the recursive nature of rapply. We begin by creating a list that contains sub lists.

> 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

You would have noticed that the result is in the form of a vector, we would want to preserve the original structure.

> 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

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

> 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"

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

> 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"

One last thing before we finish. The function inside rapply can take other arguments too. Lets see an example

> a=rapply(x,function(x,y){x^y},class=c("numeric"),how="unlist",deflt="p",y=3) > a [1] "1" "8" "p"

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.

**leave a comment**for the author, please follow the link and comment on their blog:

**R – StudyTrails**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...