[R] builtin to filter a list?
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: http://www.erlang.org/doc/man/lists.html#filter-2 filter(Pred, List1) - List2 Types: 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 Thanks, Whit __ R-help@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.
Re: [R] builtin to filter a list?
Try: ?Filter e.g. Filter(function(x) x 0, x1) or using gsubfn's fn library(gsubfn) fn$Filter(~ x 0, x1) On Wed, Oct 29, 2008 at 9:06 AM, Whit Armstrong [EMAIL PROTECTED] 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: http://www.erlang.org/doc/man/lists.html#filter-2 filter(Pred, List1) - List2 Types: 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 Thanks, Whit __ R-help@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. __ R-help@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.
Re: [R] builtin to filter a list?
?Filter ... how did I miss that one? Thanks, Gabor. -Whit On Wed, Oct 29, 2008 at 9:37 AM, Gabor Grothendieck [EMAIL PROTECTED] wrote: Try: ?Filter e.g. Filter(function(x) x 0, x1) or using gsubfn's fn library(gsubfn) fn$Filter(~ x 0, x1) On Wed, Oct 29, 2008 at 9:06 AM, Whit Armstrong [EMAIL PROTECTED] 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: http://www.erlang.org/doc/man/lists.html#filter-2 filter(Pred, List1) - List2 Types: 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 Thanks, Whit __ R-help@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. __ R-help@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.