[R] c(recursive=TRUE)

From: Eric Lecoutre <lecoutre_at_stat.ucl.ac.be>
Date: Wed 15 Jun 2005 - 00:41:20 EST

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jun 15 00:59:01 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:36 EST