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? Thanks, Ram?n -- 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 http://bioinfo.cnio.es/~rdiaz
Ramon Diaz wrote:> 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?For a list L: L[!sapply(L, is.null)] Uwe Ligges
somelist[!sapply(somelist, is.null)] e.g.> s <- list(a=1, b=NULL, c=3, d=NULL, e=5) > s[!sapply(s, is.null)]$a [1] 1 $c [1] 3 $e [1] 5 It you want to confuse people, try s[sapply(s, is.null)] <- NULL which also removes the NULL components. On Mon, 14 Apr 2003, Ramon Diaz wrote:> 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?-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hello, Have you tried: lapply(your_list, na.omit) Regards, Carlos. -----Mensaje original----- De: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch]En nombre de Ramon Diaz Enviado el: lunes, 14 de abril de 2003 16:10 Para: R-Help (E-mail) Asunto: [R] removing NULL elements from a list 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? Thanks, Ram?n -- 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 http://bioinfo.cnio.es/~rdiaz ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help ### This email has been checked for all known viruses by the ### Firstnet anti-virus system - http://www.firstnet.net.uk ### Please email fav at firstnet.net.uk for details. _____ The information in this email is confidential and it may not be\... {{dropped}}
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
you can also modify the original list "in place": l[sapply(l, is.null)] <- NULL I don't understand why it works this way especially given that, for example, l[sapply(l, is.null)] <- 5 simply replaces NULL elements of the list with 5. This probably has to do with the special semantics of NULL. I'd appreciate if someone could clearify this for me. Thanks, Vadim> -----Original Message----- > From: Peter Dalgaard BSA [mailto:p.dalgaard at biostat.ku.dk] > Sent: Monday, April 14, 2003 10:07 AM > To: Peter Dalgaard BSA > Cc: R-Help (E-mail); Ramon Diaz > Subject: Re: [R] removing NULL elements from a list > > > Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk> writes: > > > l <- l[!lapply(l,is.null)] > > ... sapply, of course, as Brian and Uwe said. > > -- > 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >-------------------------------------------------- DISCLAIMER\ This e-mail, and any attachments thereto, is intende... {{dropped}}