[R] removing NULL elements from a list

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Apr 14 17:52:21 CEST 2003

Ramon Diaz <rdiaz at cnio.es> writes:

> Dear All,
> I have a list, where several components are NULL, and I'd like to obtain that 
> very same list without the NULL components (i.e., I do not want to unlist or 
> otherwise loose the rest of the list structure). I can do that with a loop, 
> but how could I do it without a loop?

How about

l <- l[!lapply(l,is.null)]

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907

More information about the R-help mailing list