# [R] Summary: recursion over list

Eric Lecoutre lecoutre at stat.ucl.ac.be
Thu Jun 13 15:00:09 CEST 2002

```Dear all,

Some days ago, I asked to the list a way to recursively handle lists
objects, so that on can apply a function only on terminal nodes.
Thank you for all the answers.

- lapply allows the user to apply a function  on all the items of a list,
supposing this is a list with a unique depth (like:
mylist<-list(a=1,b=1:2,c=1:100,d=rnorm(10))). You can't use lapply for
deepest lists.

- unlist basically turns any list into a vector, trying to transfortm any
element into atomics then concatenating all those elements (according to my
understanding). The option "recursive' can be set to false so that only
first rank (depth) objects are treated this way. This function wasn't
suiting me, as it turns matrix into vectors.

- John Fox send a function of his that allows to flatten a list, ie to
change a generic any-depth list to a one-depth list:

flatten <- function(x){
result <- NULL
for(i in seq(along=x)) {
if (any(sapply(x[[i]], is.list))) Recall(x[[i]])
else result <- c(result, if (is.list(x[[i]])) x[[i]] else list(x[[i]]))
}
return(result)
}

This function might be usefull for some purpose, but again not mine. (as
the result is a concatenation, it can't deal matrices or functions).

- Thomas Lumley proposes a version that allows to do the same job without
having to flatten the list:

treeapply<-function (x, FUN)
{
if (is.atomic(x))  	FUN(x)
else lapply(x, function(n) treeapply(n, FUN))
}

Once again, all elements are not handled: I am considering any possible
list: a list containing a function would not be handled.

- Finally, I have to thank particularly Mark Bravington, for his detailled
answer and the code going with it.  I don't copy all this code here, as it
is long, but it allows exactly to do what I wanted and I can send it to
anyone interested.
Basically, you could find this code at:
http://r-bugs.biostat.ku.dk/cgi-bin/R/feature%26FAQ?id=1558;user=guest

As Mark, I want to complain about the lack of indicing lists with long
length indices, as it is possible in S. This features really should be
implemented in R, as it allows fast and easy recursion.

Finally, I mention some problems I still encounter with lists:

- an empty list [cf list()] is not atomic and is recursive. So when you
have to handle a list containing a node with an empty list, you may have
problems (consider the object of class lm, and it's attributes \$xlevels,
when the variable is numeric).

- Here is an example of complicated list:

complist<-
list(a=1,b=1:2,c=matrix(rnorm(100),10),d=list(),e=list(e1=1,e2=pi,e3=list(1,2)),f=function(x)return(2*x),as.ts(1:10))

My first question is equivalent to: write a R code that prints only
terminals nodes of this function (and the print method treats differently
those nodes, as they are ts, matrix, of functions)

Eric Lecoutre

×------------------------×------------------------------------------------×
| Eric Lecoutre          | Statistics                                     |
| Voie du Roman Pays, 20 | Teaching assistant / Consultant                |
| 1348 Louvain-La-Neuve  | Université de Louvain-la-Neuve                 |
| Belgique               | lecoutre at stat.ucl.ac.be                        |
| (+32) (0)10 47 30 50   | http://www.stat.ucl.ac.be/ISpersonnel/lecoutre/|
×------------------------×------------------------------------------------×
|       We need statistical thinking, not rituals  -  Gigerenzer          |
×-------------------------------------------------------------------------×

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```