# \`filter\` with \`eachrow\`

**URL:** https://discourse.julialang.org/t/filter-with-eachrow/72466
**Category:** General Usage
**Tags:** iterators
**Created:** [December 2, 2021, 3:51pm UTC](https://discourse.julialang.org/t/filter-with-eachrow/72466 "2021-12-02T15:51:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![austinbean](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@austinbean](https://discourse.julialang.org/u/austinbean)
#### Post date: [December 2, 2021, 3:51pm UTC](https://discourse.julialang.org/t/filter-with-eachrow/72466/1 "2021-12-02T15:51:50Z")

</div>

Suppose I have an array with two columns. I want to `filter` the array based on the values of the first column, but would like what is returned by `filter` to include the second column. This method seems to be missing from `filter`.

Example:

```julia
A = rand(100,2)
filter( x-> x[1]>0.5, eachrow(A))
ERROR: MethodError: no method matching filter(::var"#78#79", ::Base.Generator{Base.OneTo{Int64}, Base.var"#191#192"{Matrix{Float64}}})

```

I can do

```julia
map( x->x[1] > 0.5, eachrow(A))

```

But to do

```julia
A[map( x->x[1] > 0.5, eachrow(A)), :]

```

seems cumbersome. Is there something I’m missing about `filter` or `eachrow` that would make the option directly using `filter` work? Or was this method excluded for a reason I’m not seeing?

---

<div class="post-metadata">

### Author: ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)
#### Post date: [December 2, 2021, 3:53pm UTC](https://discourse.julialang.org/t/filter-with-eachrow/72466/2 "2021-12-02T15:53:21Z")

</div>

I believe this is possible with [Iteration utilities · The Julia Language](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.filter)

```julia
using Base.Iterators
Iterators.filter( x-> x[1]>0.5, eachrow(A)) |> collect

```

or

```julia
Iterators.filter( x-> x[1]>0.5, eachrow(A)) |>
    xs -> mapreduce(transpose, vcat, xs)

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 2, 2021, 5:04pm UTC](https://discourse.julialang.org/t/filter-with-eachrow/72466/3 "2021-12-02T17:04:43Z")

</div>

> [@austinbean](#):
>
> `filter( x-> x[1]>0.5, eachrow(A))`

The following seems to get the job done too:

```julia
filter.(x-> x[1]>0.5, Ref(collect(eachrow(A))))

```

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [December 2, 2021, 7:51pm UTC](https://discourse.julialang.org/t/filter-with-eachrow/72466/4 "2021-12-02T19:51:56Z")

</div>

There are other options, but this seems short and is twice as fast:

```julia
A[view(A,:,1).>0.5, :]

```

---

<div class="post-metadata">

### Author: ![austinbean](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@austinbean](https://discourse.julialang.org/u/austinbean)
#### Post date: [December 2, 2021, 8:43pm UTC](https://discourse.julialang.org/t/filter-with-eachrow/72466/5 "2021-12-02T20:43:16Z")

</div>

Great answers. Thanks. The first one seems closest to what I needed.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [December 2, 2021, 11:28pm UTC](https://discourse.julialang.org/t/filter-with-eachrow/72466/6 "2021-12-02T23:28:02Z")

</div>

Just use `splitdims` instead of `eachrow`: it returns a vector and supports `filter`.

```julia
using SplitApplyCombine

filter(x->x[1]>0.5, splitdims(A, 1))

```

or `splitdimsview` to get views instead of copies.
