filter

filter(x, test)

Try it yourself:

See also:

Select matching values with filter

filter(x, test) returns the elements of a collection for which a test callback is true. It keeps the original order and is useful for removing invalid observations, selecting thresholds, or narrowing a list before a calculation.

Example workflow

Filter measurements to retain values greater than zero, then use count to see how many remain or mean to summarize them. Use map when every value should be transformed rather than removed, and forEach for a callback intended for side effects.

Input caveats

The callback must return a meaningful Boolean-like decision and should not unexpectedly alter the source collection. Preserve index alignment when filtering paired data sets: applying a different test to each series can destroy correspondence before corr. For matrix slices, confirm the data structure first with size.

Try Filter in Calcul.io

Start with one of the editable examples above, then replace its arguments with your own values. Keeping the function on a separate calculator line makes the input and result easy to compare. For a longer workflow, assign the result to a variable or reference that line in the next expression.

Check the shown signature before adding optional arguments, and use the related-function links to compare operations with similar purposes.

All functions