[R] How to flatten a tree (based on list) to a certain depth?

Peng Yu pengyu.ut at gmail.com
Wed Feb 3 21:01:37 CET 2010


On Wed, Feb 3, 2010 at 1:47 PM, Bert Gunter <gunter.berton at gene.com> wrote:
> -- and you didn't read my reply.

I did. You said to use a loop. I think flatten a tree to a certain
level shouldn't be done with a loop. Loop is a way used in the
imperative programming style. Flatten is in the functional programming
style. I don't think that it is a good idea to mix the two.

Also, the flatten a tree to a certain level operation should be
extracted to a well defined function for re-usability. Using a loop is
a bad idea to enhance re-usability.


>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of Peng Yu
> Sent: Wednesday, February 03, 2010 11:40 AM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] How to flatten a tree (based on list) to a certain depth?
>
> On Wed, Feb 3, 2010 at 12:26 PM, Bert Gunter <gunter.berton at gene.com> wrote:
>> RTFM (?unlist -- the "recursive" parameter)
>> and do it with a loop however many times you like.
>
> How do you know that I didn't read the manual? I didn't quite get my
> question. Please see my response to Steve.
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On
>> Behalf Of Peng Yu
>> Sent: Wednesday, February 03, 2010 10:20 AM
>> To: r-help at stat.math.ethz.ch
>> Subject: [R] How to flatten a tree (based on list) to a certain depth?
>>
>> Suppose that I have the following list of lists of frames 'root'
>> (let's call it a 'tree' of frames). I want to flatten it to be a list
>> of frames. However, if I unlist(root), it will flatten the frames as
>> well. Is there a simply way to flatten the tree to certain depth?
>>
>> aframe1=data.frame(x=1:3,y=1:3)
>> aframe2=data.frame(u=7:9,v=11:13)
>> aframe3=data.frame(p=3:5,q=6:8)
>>
>> main1=list(sub1=aframe1, sub2=aframe2)
>> main2=list(sub3=aframe3)
>>
>> root=list(main1=main1, main2=main2)
>>
>> str(root)
>> unlist(root)
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list