[R] Subsetting list of vectors with list of (boolean) vectors?

Liaw, Andy andy_liaw at merck.com
Thu Apr 12 18:52:50 CEST 2007


From: Marc Schwartz
> 
> On Thu, 2007-04-12 at 18:12 +0200, Johannes Graumann wrote:
> > Dear Rologists,
> > 
> > I'm stuck with this. How would you do this efficiently:
> > 
> > > aPGI
> > [[1]]
> > [1] "864"  "5576"
> > 
> > 
> > > aPGItest
> > [[1]]
> > [1]  TRUE FALSE
> > 
> > > result <- [magic box involving subset)
> > 
> > > result
> > [[1]]
> > [1] "864"
> > 
> > Thanks for any hints,
> > 
> > Joh
> 
> 
> > lapply(seq(along = length(aPGI)), function(x) 
> > aPGI[[x]][aPGItest[[x]]])
> [[1]]
> [1] "864"

Alternatively:

R> mapply("[", aPGI, aPGItest, SIMPLIFY=FALSE)
[[1]]
[1] "864"

Cheers,
Andy

 
> 
> I think that this should be a generic solution for multiple 
> (but common) levels in each list.
> 
> HTH,
> 
> Marc Schwartz
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}



More information about the R-help mailing list