[R] Flattening lists and environments (was: "how to flatten a list to the same level?")
Janko Thyson
janko.thyson.rstuff at googlemail.com
Thu May 19 22:40:44 CEST 2011
Dear list,
I came up with a two functions that flatten arbitrary deeply nested
lists (as long as they're named; not tested for unnamed) and
environments (see attachment; 'flatten_examples.txt' contains some
examples).
The paradigm is somewhat similar to that implemented in 'unlist()', yet
extends it. I would have very much liked to build upon the superfast
functionality of 'unlist()', but there are some obstacles to this (see
these two related posts at r-devel from today:
https://stat.ethz.ch/pipermail/r-devel/2011-May/061070.html and
https://stat.ethz.ch/pipermail/r-devel/2011-May/061071.html).
Therefore, I had to use a recursive looping paradigm. Yet, if anyone has
some suggestions on how to speed things up (maybe some Rcpp-people feel
"called upon"?!? ;-)), I'd appreciate any pointers. Yet I do hope that
what I came up with is at least of some value for those that posted
similar questions on how to flexibly flatten nested objects in the past
(that's why I'm also referring to this older post below; I also build
upon the code provided by Henrique Dallazuanna and Mark Heckmann).
Best regards,
Janko
PS: Maybe this should rather go into a blog-post, but I don't have one
yet ;-)
On 19.05.2011 22:16, Janko Thyson wrote:
> From: Mark Heckmann <mark.heckmann_at_gmx.de
> <mailto:mark.heckmann_at_gmx.de?Subject=Re:%20[R]%20how%20to%20flatten%20a%20list%20to%20the%20same%20level?>>
>
> Date: Sat, 09 Jan 2010 13:49:15 +0100
>
> Henrique,
>
> thanks for the code!! It works out fine for vectors. I forgot to
> mention I also have dataframes as list elements. Thus I want the
> structure of the list element to be kept intact.
>
> I tried an recursive approach (which unfortunately resulted in some
> more code) which works.
>
> .getNonListElements <- function(x, env){
>
> if(class(x)=="list") {
> for(i in seq(along=x)) .getNonListElements(x[[i]], env) # call
> recursively
> } else {
> res<- get("res", envir = env) # get res from other env
> res<- c(res, list(x)) # add one list element
> assign("res", res, envir=env) # assign back to env
> }
>
> }
>
> flattenList <- function(l){
>
> res<- list() # make list object
> env<- environment() # get current env
> .getNonListElements(l, env) # search for non list elements recursively
> return(res)
>
> }
>
> l <- list(DF=data.frame(A=c(1,2)), vec=c("a", "b")) l <- list(l,l)
>
> > flattenList(l)
>
> [[1]]
>
> A
> 1 1
> 2 2
>
> [[2]]
> [1] "a" "b"
>
> [[3]]
>
> A
> 1 1
> 2 2
>
> [[4]]
> [1] "a" "b"
>
> I am not sure if one can avoid the wrapper function or still use
> rapply to simplify the code. I do not know how. One more thing I would
> like to add are the objects names to the generated list. But I did not
> succeed in that.
>
> Mark
>
> Am 08.01.2010 um 18:29 schrieb Henrique Dallazuanna:
>
> > Try something about like this:
> >
> > split(unlist(l), rep(1:length(idx<- rapply(l, length)), idx))
> >
> > On Fri, Jan 8, 2010 at 1:35 PM, Mark Heckmann<mark.heckmann_at_gmx.de>
> > wrote:
> >> I have a nested list l like:
> >>
> >> l<- list(A=c(1,2,3), B=c("a", "b"))
> >> l<- list(l,l, list(l,l))
> >>
> >> I want the list to be unlisted, but not on the lowest level of each
> >> "branch".
> >> I want the lowest level of each list branch to remain as it is.
> >> So unlist or unlist(rec=F) do not work here as the level of nesting
> >> may
> >> differ on the elements.
> >> The result should look like:
> >>
> >> $A
> >> [1] 1 2 3
> >>
> >> $B
> >> [1] "a" "b"
> >>
> >> $A
> >> [1] 1 2 3
> >>
> >> $B
> >> [1] "a" "b"
> >>
> >> $A
> >> [1] 1 2 3
> >>
> >> $B
> >> [1] "a" "b"
> >>
> >> $A
> >> [1] 1 2 3
> >>
> >> $B
> >> [1] "a" "b"
> >>
> >> Any ideas?
> >> TIA!
> >>
> >> Mark
> >>
> >>
> >> ------------------------------------------------------------------------------
> >> Mark Heckmann
> >> Dipl. Wirt.-Ing. cand. Psych.
> >> Vorstraße 93 B01
> >> 28359 Bremen
> >> Blog:www.markheckmann.de
> >> R-Blog:http://ryouready.wordpress.com
> >>
> >> ______________________________________________
> >> R-help_at_r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >
> >
> > --
> > Henrique Dallazuanna
> > Curitiba-Paraná-Brasil
> > 25° 25' 40" S 49° 16' 22" O
>
> ------------------------------------------------------------------------------
>
> Mark Heckmann
> Dipl. Wirt.-Ing. cand. Psych.
> Vorstraße 93 B01
> 28359 Bremen
> Blog: www.markheckmann.de
> R-Blog: http://ryouready.wordpress.com
> --
> ------------------------------------------------------------------------
>
> *Janko Thyson*
> janko.thyson at ku-eichstaett.de <mailto:janko.thyson at ku-eichstaett.de>
>
> Catholic University of Eichstätt-Ingolstadt
> Ingolstadt School of Management
> Statistics and Quantitative Methods
> Auf der Schanz 49
> D-85049 Ingolstadt
>
> www.wfi.edu/lsqm <http://www.wfi.edu/lsqm>
>
> Fon: +49 841 937-1923
> Fax: +49 841 937-1965
>
> This e-mail and any attachment is for authorized use by the intended
> recipient(s) only. It may contain proprietary material, confidential
> information and/or be subject to legal privilege. It should not be
> copied, disclosed to, retained or used by any other party.
> If you are not an intended recipient then please promptly delete this
> e-mail and any attachment and all copies and inform the sender.
>
--
------------------------------------------------------------------------
*Janko Thyson*
janko.thyson at googlemail.com <mailto:janko.thyson at googlemail.com>
Jesuitenstraße 3
D-85049 Ingolstadt
Mobile: +49 (0)176 83294257
This e-mail and any attachment is for authorized use by the intended
recipient(s) only. It may contain proprietary material, confidential
information and/or be subject to legal privilege. It should not be
copied, disclosed to, retained or used by any other party.
If you are not an intended recipient then please promptly delete this
e-mail and any attachment and all copies and inform the sender.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: envirAsList.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110519/81044464/attachment.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: flatten.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110519/81044464/attachment-0001.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: flatten_examples.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110519/81044464/attachment-0002.txt>
More information about the R-help
mailing list