Peng Yu
2010-Feb-03 18:19 UTC
[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)
Steve Lianoglou
2010-Feb-03 18:29 UTC
[R] How to flatten a tree (based on list) to a certain depth?
Hi, On Wed, Feb 3, 2010 at 1:19 PM, Peng Yu <pengyu.ut at gmail.com> wrote:> 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)Does `unlist(root, recursive=FALSE)` get you what you want? R> unlist(root, recursive=FALSE) $main1.sub1 x y 1 1 1 2 2 2 3 3 3 $main1.sub2 u v 1 7 11 2 8 12 3 9 13 $main2.sub3 p q 1 3 6 2 4 7 3 5 8 -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
Peng Yu
2010-Feb-03 19:39 UTC
[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. > >
Peng Yu
2010-Feb-03 20:01 UTC
[R] How to flatten a tree (based on list) to a certain depth?
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. > >
Seemingly Similar Threads
- converting the string columns in a data.frame to factors?
- IRMover asserts "mapping to a source type" when repeatedly linking - usage or LLVM bug?
- IRMover asserts "mapping to a source type" when repeatedly linking - usage or LLVM bug?
- IRMover asserts "mapping to a source type" when repeatedly linking - usage or LLVM bug?
- IRMover asserts "mapping to a source type" when repeatedly linking - usage or LLVM bug?