[R] builtin to filter a list?

Whit Armstrong armstrong.whit at gmail.com
Wed Oct 29 14:06:59 CET 2008

I know it's easy to write a simple loop to do this, but in the spirit
of lapply, I thought I would ask if there is a builtin to filter or
take a subset of a list based on a predicate in a similar way to the
Erlang lists:filter/2 function:


filter(Pred, List1) -> List2

Pred = fun(Elem) -> bool()
 Elem = term()
List1 = List2 = [term()]
List2 is a list of all elements Elem in List1 for which Pred(Elem) returns true.

I've tried the simple case in R already:

> x <- rnorm(10)
> xl <- as.list(x)
> xl[[ x > 0]]
Error in xl[[x > 0]] : attempt to select less than one element


More information about the R-help mailing list