[R] flatten lists

Jeroen Ooms jeroen.ooms at stat.ucla.edu
Tue Jun 26 23:25:40 CEST 2012


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?



More information about the R-help mailing list