[R] builtin to filter a list?
armstrong.whit at gmail.com
Wed Oct 29 15:03:33 CET 2008
how did I miss that one?
On Wed, Oct 29, 2008 at 9:37 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Filter(function(x) x > 0, x1)
> or using gsubfn's fn
> fn$Filter(~ x > 0, x1)
> On Wed, Oct 29, 2008 at 9:06 AM, Whit Armstrong
> <armstrong.whit at gmail.com> wrote:
>> 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
>> R-help at r-project.org mailing list
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help