# Byrow with user defined function

**URL:** https://discourse.julialang.org/t/byrow-with-user-defined-function/79132
**Category:** Data
**Tags:** question, inmemorydatasets
**Created:** [April 6, 2022, 11:55pm UTC](https://discourse.julialang.org/t/byrow-with-user-defined-function/79132 "2022-04-06T23:55:43Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![monopolynomial](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/monopolynomial/32/34782_2.png) [@monopolynomial](https://discourse.julialang.org/u/monopolynomial)
#### Post date: [April 6, 2022, 11:55pm UTC](https://discourse.julialang.org/t/byrow-with-user-defined-function/79132/1 "2022-04-06T23:55:44Z")

</div>

for data like

```julia
ds=Dataset(x1=[1,2,3],x2=[3,2,1])

```

if I want to filter rows where x1 is less than x2 then this works

```julia
filter(ds, :x1, type=isless, with = :x2)

```

but when I define a function for `byrow` like

```julia
f(x)=x[1]< x[2]

byrow(ds, f, :) # works

```

I cannot use it with `filter`

```julia
filter(ds, :, type=f)
ERROR: BoundsError: attempt to access 3×2 Dataset at index [Union{Missing, Bool}[true, false, false], :]
Stacktrace:

```

---

<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: [April 7, 2022, 10:13am UTC](https://discourse.julialang.org/t/byrow-with-user-defined-function/79132/2 "2022-04-07T10:13:48Z")

</div>

after reading that  
" … `filter(ds, cols; [view = false, type = all,...])` is the shortcut for `ds[byrow(ds, type, cols; ...), :]` …"

```julia
julia> byrow(ds, f, :)# works
3-element Vector{Union{Missing, Bool}}:
  true
 false
 false

```

I have tried the following expressions, which I would expect to give the same result.  
but it is not so, as rightly observed by @monopolynomial

```julia
julia> ds[[true,false,false],:]
1×2 Dataset
 Row │ x1 x2       
     │ identity identity
     │ Int64? Int64?
─────┼────────────────────
   1 │ 1 3

julia> ds[byrow(ds, f, :),:]
ERROR: BoundsError: attempt to access 3×2 Dataset at index [Union{Missing, Bool}[true, false, false], :]

```

" Naturally, other `fun` s supported by `byrow` which return a `Vector{Bool}` or `BitVector` can be used to filter observations, too."

the problem therefore seems to be that `byrow (...)` only works with `Vector{Bool}` but not with `Vector{Union{Missing, Bool}}`

this “works”

```julia
julia> ds[Bool.(byrow(ds, f, :)),:]
1×2 Dataset
 Row │ x1 x2       
     │ identity identity 
     │ Int64? Int64?   
─────┼────────────────────
   1 │ 1 3

```

the issue seems to be the handling of `Union {Missing, T}` types

---

<div class="post-metadata">

### Author: ![monopolynomial](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/monopolynomial/32/34782_2.png) [@monopolynomial](https://discourse.julialang.org/u/monopolynomial)
#### Post date: [April 9, 2022, 1:43am UTC](https://discourse.julialang.org/t/byrow-with-user-defined-function/79132/3 "2022-04-09T01:43:55Z")

</div>

```julia
julia> ds[Bool.(byrow(ds, f, :)),:]

```

This doesn’t work if `f` returns missing.

---

<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: [April 9, 2022, 10:28am UTC](https://discourse.julialang.org/t/byrow-with-user-defined-function/79132/4 "2022-04-09T10:28:20Z")

</div>

Yes.  
I have seen and, in part, I had even foreseen it.  
But I didn’t want to linger.  
Perhaps, in the general case, a function would be needed to establish on a case-by-case basis whether missing is to be evaluated as true or false (or simply remain missing)…  
Maybe a kwarg that can be set by the user as needed could do the trick.

---

<div class="post-metadata">

### Author: ![Kia\_Kia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kia_kia/32/35863_2.png) [@Kia\_Kia](https://discourse.julialang.org/u/Kia_Kia)
#### Post date: [May 1, 2022, 10:32am UTC](https://discourse.julialang.org/t/byrow-with-user-defined-function/79132/5 "2022-05-01T10:32:55Z")

</div>

findall is good for this.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 25, 2022, 4:41pm UTC](https://discourse.julialang.org/t/byrow-with-user-defined-function/79132/6 "2022-08-25T16:41:36Z")

</div>

A post was split to a new topic: [Help with understanding Julia types](https://discourse.julialang.org/t/help-with-understanding-julia-types/86345)
