# filter() Function & Examples

Use the filter() matrix function in Calcul.io. Review its syntax, edit working examples, understand the result, and explore related math functions.

## filter

#### filter(x, test)

Try it yourself:

```calculio
isPositive(x) = x > 0
filter([6, -2, -1, 4, 3], isPositive)
filter([6, -2, 0, 1, 0], x != 0)
```

[sort](https://calcul.io/function/sort/index.md)

[map](https://calcul.io/function/map/index.md)

---

## 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](https://calcul.io/function/count/index.md) to see how many remain or [mean](https://calcul.io/function/mean/index.md) to summarize them. Use [map](https://calcul.io/function/map/index.md) when every value should be transformed rather than removed, and [forEach](https://calcul.io/function/forEach/index.md) 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](https://calcul.io/function/corr/index.md). For matrix slices, confirm the data structure first with [size](https://calcul.io/function/size/index.md).

[All functions](https://calcul.io/functions/index.md)
