tree manipulation
1
0
Entering edit mode
@anthony-ferrari-3991
Last seen 9.7 years ago
Hi all, Does exist a package that allows one to recursively apply a custom function to nodes in a binary tree (from hclust) from leaves to the root ? A kind of "tree_apply" ? Ideally, after that, we could easily retrieve the value of the function at each node and the leaves IDs under that node. Thank you. AF [[alternative HTML version deleted]]
• 859 views
ADD COMMENT
0
Entering edit mode
@wolfgang-huber-3550
Last seen 5 weeks ago
EMBL European Molecular Biology Laborat…
On 11/05/10 14:49, Anthony Ferrari wrote: > Hi all, > > Does exist a package that allows one to recursively apply a custom function > to nodes in a binary tree (from hclust) from leaves to the root ? > A kind of "tree_apply" ? > Ideally, after that, we could easily retrieve the value of the function at > each node and the leaves IDs under that node. > > Thank you. > > AF > > Dear Anthony, /* this would be better for R-help.... */ perhaps there is already a solution out there, but the task is so simple that you may not need a package. For trees that are represented as lists of lists, something like the below code example would work. For the representation used by hclust, you could write a similar function that loops over the rows of the "merge" matrix (see the hclust man page). tree = list(a = list( A=1, B=2), b = list( A=list(x=3, y=4, z=5), B=list(x=6, y=7), C=8), c = 9) traverse = function(x, f) if (is.list(x)) lapply(x, traverse, f=f) else f(x) traverse(tree, f=function(x) x+1) Wolfgang Huber EMBL http://www.embl.de/research/units/genome_biology/huber
ADD COMMENT
0
Entering edit mode
Try: ?dendrapply Thomas On Wed, May 12, 2010 at 12:34:11AM +0200, Wolfgang Huber wrote: > On 11/05/10 14:49, Anthony Ferrari wrote: > >Hi all, > > > >Does exist a package that allows one to recursively apply a custom > >function > >to nodes in a binary tree (from hclust) from leaves to the root ? > >A kind of "tree_apply" ? > >Ideally, after that, we could easily retrieve the value of the function at > >each node and the leaves IDs under that node. > > > >Thank you. > > > >AF > > > > > > Dear Anthony, > > /* this would be better for R-help.... */ > > perhaps there is already a solution out there, but the task is so simple > that you may not need a package. > > For trees that are represented as lists of lists, something like the > below code example would work. > > For the representation used by hclust, you could write a similar > function that loops over the rows of the "merge" matrix (see the hclust > man page). > > > tree = list(a = list( A=1, B=2), > b = list( A=list(x=3, y=4, z=5), > B=list(x=6, y=7), > C=8), > c = 9) > > traverse = function(x, f) > if (is.list(x)) lapply(x, traverse, f=f) else f(x) > > traverse(tree, f=function(x) x+1) > > > > > Wolfgang Huber > EMBL > http://www.embl.de/research/units/genome_biology/huber > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor >
ADD REPLY
0
Entering edit mode
Thanks Wolfgang and Thomas. Indeed, looping recursively over the rows of the merge matrix is simple. On 12 May 2010 01:56, Thomas Girke <thomas.girke@ucr.edu> wrote: > Try: > > ?dendrapply > > Thomas > > On Wed, May 12, 2010 at 12:34:11AM +0200, Wolfgang Huber wrote: > > On 11/05/10 14:49, Anthony Ferrari wrote: > > >Hi all, > > > > > >Does exist a package that allows one to recursively apply a custom > > >function > > >to nodes in a binary tree (from hclust) from leaves to the root ? > > >A kind of "tree_apply" ? > > >Ideally, after that, we could easily retrieve the value of the function > at > > >each node and the leaves IDs under that node. > > > > > >Thank you. > > > > > >AF > > > > > > > > > > Dear Anthony, > > > > /* this would be better for R-help.... */ > > > > perhaps there is already a solution out there, but the task is so simple > > that you may not need a package. > > > > For trees that are represented as lists of lists, something like the > > below code example would work. > > > > For the representation used by hclust, you could write a similar > > function that loops over the rows of the "merge" matrix (see the hclust > > man page). > > > > > > tree = list(a = list( A=1, B=2), > > b = list( A=list(x=3, y=4, z=5), > > B=list(x=6, y=7), > > C=8), > > c = 9) > > > > traverse = function(x, f) > > if (is.list(x)) lapply(x, traverse, f=f) else f(x) > > > > traverse(tree, f=function(x) x+1) > > > > > > > > > > Wolfgang Huber > > EMBL > > http://www.embl.de/research/units/genome_biology/huber > > > > _______________________________________________ > > Bioconductor mailing list > > Bioconductor@stat.math.ethz.ch > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > Search the archives: > > http://news.gmane.org/gmane.science.biology.informatics.conductor > > > > _______________________________________________ > Bioconductor mailing list > Bioconductor@stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > [[alternative HTML version deleted]]
ADD REPLY

Login before adding your answer.

Traffic: 732 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6