I am looking for a function to flatten a list to a list of only 1
level deep. Very similar to unlist, however I don't want to turn it
into a vector because then everything will be casted to character
vectors:
x <- list(name="Jeroen", age=27, married=FALSE,
home=list(country="Netherlands", city="Utrecht"))
unlist(x)
This function sort of does it:
flatlist <- function(mylist){
lapply(rapply(mylist, enquote, how="unlist"), eval)
}
flatlist(x)
However it is a bit slow. Is there a more native way?
do.call("c", x)
maybe?
On Tue, Jun 26, 2012 at 02:25:40PM -0700, Jeroen Ooms
wrote:> I am looking for a function to flatten a list to a list of only 1
> level deep. Very similar to unlist, however I don't want to turn it
> into a vector because then everything will be casted to character
> vectors:
>
> x <- list(name="Jeroen", age=27, married=FALSE,
> home=list(country="Netherlands", city="Utrecht"))
> unlist(x)
>
> This function sort of does it:
>
> flatlist <- function(mylist){
> lapply(rapply(mylist, enquote, how="unlist"), eval)
> }
>
> flatlist(x)
>
> However it is a bit slow. Is there a more native way?
>
> ______________________________________________
> 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.
Frankly, I'm not sure what you mean, but presumably unlist(yourlist, recurs=FALSE) is not it, right? -- Bert On Tue, Jun 26, 2012 at 2:25 PM, Jeroen Ooms <jeroen.ooms@stat.ucla.edu>wrote:> I am looking for a function to flatten a list to a list of only 1 > level deep. Very similar to unlist, however I don't want to turn it > into a vector because then everything will be casted to character > vectors: > > x <- list(name="Jeroen", age=27, married=FALSE, > home=list(country="Netherlands", city="Utrecht")) > unlist(x) > > This function sort of does it: > > flatlist <- function(mylist){ > lapply(rapply(mylist, enquote, how="unlist"), eval) > } > > flatlist(x) > > However it is a bit slow. Is there a more native way? > > ______________________________________________ > R-help@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. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]