[R] subset list based on logical within element flag

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Mar 12 23:03:46 CET 2008


I'm not sure exactly what you want, but try something like

lapply(gene.pair.tf.lst, function(x) if(x$sig.cor) x$gene.pair)

If you only want the non-NULL entries (and how would you know which they 
are, given you have duplicates?)

gene.pair.tf.lst <- list(list(gene.pair="Lgals1:Pxmp2", sig.cor=FALSE),
     list(gene.pair="Lgals1:Pxmp2", sig.cor=TRUE),
     list(gene.pair="Lgals1:Pxmp2", sig.cor=FALSE))
tmp <- lapply(gene.pair.tf.lst, function(x) if(x$sig.cor) x$gene.pair)
tmp[!sapply(tmp, is.null)]


On Wed, 12 Mar 2008, Mark W Kimpel wrote:

> I have a very long list that I'd like to subset based on a logical value
> within each element. Example below. I'd like to get just those list
> elements for further study whose $sig.cor slot is TRUE. In this example,
> I'd only want element [[2]].
>
> Should be simple, I know. How can I do this? Thanks, Mark
>
> > gene.pair.tf.lst
> [[1]]
> [[1]]$gene.pair
> [1] "Lgals1:Pxmp2"
>
> [[1]]$sig.cor
> [1] FALSE
>
>
> [[2]]
> [[2]]$gene.pair
> [1] "Lgals1:Pxmp2"
>
> [[2]]$sig.cor
> [1] TRUE
>
>
> [[3]]
> [[3]]$gene.pair
> [1] "Lgals1:Pxmp2"
>
> [[3]]$sig.cor
> [1] FALSE
> --
>
> Mark W. Kimpel MD  ** Neuroinformatics ** Dept. of Psychiatry
> Indiana University School of Medicine
>
> 15032 Hunter Court, Westfield, IN  46074
>
> (317) 490-5129 Work, & Mobile & VoiceMail
> (317) 204-4202 Home (no voice mail please)
>
> mwkimpel<at>gmail<dot>com
>
> ______________________________________________
> 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.
>

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



More information about the R-help mailing list