# Filterslices or selectslices

**URL:** <https://discourse.julialang.org/t/filterslices-or-selectslices/111746>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 18, 2024, 4:09am UTC](https://discourse.julialang.org/t/filterslices-or-selectslices/111746 "2024-03-18T04:09:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 18, 2024, 4:09am UTC](https://discourse.julialang.org/t/filterslices-or-selectslices/111746/1 "2024-03-18T04:09:28Z")

</div>

Are there functions like `filterslices` or `selectslices` to do the below.

```julia
X =[
        1 1 1 1 
        1 missing 1 1 
        1 missing missing 1
        1 1 1 missing
    ]

i = mapslices( x-> count(ismissing,x) ≤1, X , dims=1)[1,:]
X[:,i]

#something like this to do the above
filterslices( x-> count(ismissing,x) ≤1, X, dims=1 )

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [March 18, 2024, 10:21am UTC](https://discourse.julialang.org/t/filterslices-or-selectslices/111746/2 "2024-03-18T10:21:37Z")

</div>

Maybe these are slightly more readable?

```julia
stack(filter(x->count(ismissing,x)<=1, eachcol(X)))

stack(x for x in eachcol(X) if count(ismissing,x)<=1) # Iterators.filter

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 20, 2024, 8:36am UTC](https://discourse.julialang.org/t/filterslices-or-selectslices/111746/3 "2024-03-20T08:36:16Z")

</div>

other possibility:

```julia
X[:,findall(x-> count(ismissing,x)<=1,eachcol(X))]

```

and this too (although not very beautiful and efficient)

```julia
[filter(x-> count(ismissing,x)<=1,eachcol(X))... ;;]

```

---

<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:** [March 20, 2024, 8:39am UTC](https://discourse.julialang.org/t/filterslices-or-selectslices/111746/4 "2024-03-20T08:39:39Z")

</div>

```julia
using AccessorsExtra

@modify(eachcol(X)) do cols
    filter(x -> count(ismissing, x) ≤ 1, cols)
end

```

works with eachrow and eachslice as well, not just eachcol.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 20, 2024, 11:06am UTC](https://discourse.julialang.org/t/filterslices-or-selectslices/111746/5 "2024-03-20T11:06:11Z")

</div>

Thanks all. Very useful  
Its actually cleaner with a KeyedArray

```julia
X = KeyedArray( [
    #b1 #b2
    1;1;0;1;; 1;0;1;1;;; #c1
    1;0;1;1;; 1;1;1;1;;; #c2
    1;1;0;1;; 1;1;1;1 #c3
], a=1:4, b=[:b1,:b2], c=[:c1,:c2,:c3] )

X(c = [i for i in X.c if count(iszero, X(c=i)) ≤ 1] )

```
