[R] c(recursive=TRUE)

Eric Lecoutre lecoutre at stat.ucl.ac.be
Tue Jun 14 16:41:20 CEST 2005



Hi R users,

I am currently using c(...,recursive=TRUE) to handle list-structured
objects.
This allows to represent something like:

> l1 = list(level1=1,level2=list(sub1=1,sub2=2))
as:
> (l1names = names(c(l1,recursive=TRUE)))
[1] "level1"      "level2.sub1" "level2.sub2"

Then, one can use:
> (l1names = sapply(l1names,FUN=function(element)
strsplit(element,"\\.")))
$level1
[1] "level1"

$level2.sub1
[1] "level2" "sub1"  

$level2.sub2
[1] "level2" "sub2"  


Which allows to access to individuals terminal nodes of list using
indexing, as 
l1[[c("level2","sub2")]] nicely works in R.

For my very specific uses, it unfortunatelt happens that some elements
of my list do have names that contains a dot (for example:
decimal.mark). This is very frustating as a consequence is that my
function does not work in that case...

One workaround would be to add an argument to c(), as delimitor='.' by
default, allowing to put a less usual delimitor (say "§"). But this
would mean changing in C (feature request?). Same for "unlist".

Is there any other easy way to get the structure of a list?

Eric




Eric Lecoutre
UCL /  Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium

tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre

If the statistics are boring, then you've got the wrong numbers. -Edward
Tufte




More information about the R-help mailing list