[R] removing NULL elements from a list: Thanks

Ramon Diaz rdiaz at cnio.es
Tue Apr 15 10:25:55 CEST 2003

Thank you very much for the very helpful responses from D. Bates, P. Dalgaard, 
J. Holtman, D. James, U. Ligges, V. Ogranovich, C. Ortega, B. D. Ripley and 
T. Plate. 

The original question is at the end.
Most of the answers were: (A is the list, with the NULLs I want to get rid of)

A[!sapply(A, is.null)]

A[!unlist(lapply(A, is.null))]

A[sapply(A, is.null)] <- NULL ## potentially confussing, as pointed out by 
B.D. Ripley

Thanks again,


> 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?

Ramón Díaz-Uriarte
Bioinformatics Unit
Centro Nacional de Investigaciones Oncológicas (CNIO)
(Spanish National Cancer Center)
Melchor Fernández Almagro, 3
28029 Madrid (Spain)
Fax: +-34-91-224-6972
Phone: +-34-91-224-6900


More information about the R-help mailing list