# DataFramesMeta.jl insert @where subset programmatically?

**URL:** <https://discourse.julialang.org/t/dataframesmeta-jl-insert-where-subset-programmatically/6407>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 12, 2017, 10:03am UTC](https://discourse.julialang.org/t/dataframesmeta-jl-insert-where-subset-programmatically/6407 "2017-10-12T10:03:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [October 12, 2017, 10:03am UTC](https://discourse.julialang.org/t/dataframesmeta-jl-insert-where-subset-programmatically/6407/1 "2017-10-12T10:03:07Z")

</div>

```julia
using DataFrames, DataFramesMeta
# this works
y = DataFrame(a=[1,2,3],b=rand(3),c=rand(3))
@where(y,:a.>2)

# how to achieve that?
s = :(:a.<2) # this criterion will change
@where(y,s)

# i tried
julia> macro wwhere(y,s)
       return :( @where(y, ^(s)) )
       end
julia> @wwhere y s
ERROR: MethodError: no method matching ^(::Expr)

# and
julia> macro wwhere(y,s)
       return :( @where(y, ^($s)) )
       end
julia> @wwhere y s
ERROR: MethodError: no method matching ^(::Expr)

```

I have a function that needs to subset to different things, so this functionality would be very useful for me.

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [October 12, 2017, 2:06pm UTC](https://discourse.julialang.org/t/dataframesmeta-jl-insert-where-subset-programmatically/6407/2 "2017-10-12T14:06:03Z")

</div>

ok i made a little progress but still not there completely. how can I pass into the function the `isless` operation I want to do?

```julia
julia> f = function(y,var)
       @where(y,_I_(var).<2)
       end
(::#45) (generic function with 1 method)

julia> f(y,:a)
1×3 DataFrames.DataFrame
│ Row │ a │ b │ c │
├─────┼───┼─────────┼──────────┤
│ 1 │ 1 │ 0.86181 │ 0.295221 │

```

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [October 12, 2017, 2:20pm UTC](https://discourse.julialang.org/t/dataframesmeta-jl-insert-where-subset-programmatically/6407/3 "2017-10-12T14:20:38Z")

</div>

Maybe this does what you want:

```julia
g = t -> t.>2
f = function(y, var, sel_func)
    @where(y, sel_func(_I_(var)))
end

f(y, :a, g)

```

Though to be entirely honest, it’s actually very easy to implement your own select function in a reasonably efficient way by extracting columns and then broadcasting your intended conditions over them. See for example [this code](https://github.com/piever/PlugAndPlot.jl/blob/master/src/select_functions.jl#L13). Line 13 to 23 is already enough for what you need, you get a function `choose_data` that takes as input the DataFrame and a Dict, where at symbol `s` you associate the predicate (a function returning a boolean) to be applied to the column `df[s]`

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [October 12, 2017, 2:47pm UTC](https://discourse.julialang.org/t/dataframesmeta-jl-insert-where-subset-programmatically/6407/4 "2017-10-12T14:47:11Z")

</div>

great! exactly what I wanted. i like your second idea as well, but it doesn’t work so well for me as this is embedded in a @linq pipeline of operations. great package by the way!

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [October 12, 2017, 3:35pm UTC](https://discourse.julialang.org/t/dataframesmeta-jl-insert-where-subset-programmatically/6407/5 "2017-10-12T15:35:00Z")

</div>

Glad you like PlugAndPlot!

Personally, I prefer the simple pipeline syntax with `@>` as it’s less “magic” (it’s also mentioned on the DataFramesMeta README as an alternative to `@linq`). If you use the `@>` syntax you can actually add normal functions in between, as long as they take the DataFrame as first argument and output a DataFrame (unless they are at the end of the pipeline and then they can output whatever, e.g. a plot or a different way of summarizing your data).

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [October 12, 2017, 3:36pm UTC](https://discourse.julialang.org/t/dataframesmeta-jl-insert-where-subset-programmatically/6407/6 "2017-10-12T15:36:54Z")

</div>

ok I see! that sounds pretty cool actually. I should look into that. it’s so easy to get stuck with what “works” at some point. thanks again!
