Would it be appropriate to add a Which higher-order function?

Which <- function(f, x) {
   ind <- vapply(x, f, logical(1))
   !is.na(ind) & ind
}

Then Filter would be:

Filter <- function(f, x) x[Which(f, x)]

I think the use of vapply is slightly better than the current
as.logical(sapply(x, f)) because it will return a more informative
error message if the predicate doesn't return a logical vector of the
correct length.

Hadley



-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to