# Query.jl: how to filter on derived quantities of a group?

**URL:** <https://discourse.julialang.org/t/query-jl-how-to-filter-on-derived-quantities-of-a-group/23486>\
**Category:** Data\
**Created:** [April 24, 2019, 9:19pm UTC](https://discourse.julialang.org/t/query-jl-how-to-filter-on-derived-quantities-of-a-group/23486 "2019-04-24T21:19:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gajomi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gajomi/32/7655_2.png) [@gajomi](https://discourse.julialang.org/u/gajomi)\
**Post date:** [April 24, 2019, 9:19pm UTC](https://discourse.julialang.org/t/query-jl-how-to-filter-on-derived-quantities-of-a-group/23486/1 "2019-04-24T21:19:33Z")

</div>

I am having a hard time trying to figure out how to filter by a derived quantity of a group using either the standalone or linq syntax. For example, I might want to group by some column then select only rows in that group with a second column having values larger than the group mean. Very much a split/apply/combine-y thing but can;t figure out if there is syntax to support.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [April 24, 2019, 9:26pm UTC](https://discourse.julialang.org/t/query-jl-how-to-filter-on-derived-quantities-of-a-group/23486/2 "2019-04-24T21:26:00Z")

</div>

Can you provide some test data, MWE, and expected results?

---

<div class="post-metadata">

**Author:** ![gajomi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gajomi/32/7655_2.png) [@gajomi](https://discourse.julialang.org/u/gajomi)\
**Post date:** [April 24, 2019, 10:24pm UTC](https://discourse.julialang.org/t/query-jl-how-to-filter-on-derived-quantities-of-a-group/23486/3 "2019-04-24T22:24:31Z")

</div>

Well I don’t know how to provide a MWE in the linq style… that is what I am confused about. But with just DataFrames one example might be:

```julia
data = DataFrame(x = [:a,:a,:a,:a,:b,:b,:b,:c,:c], y = 1:9, z = 11:19)
groupcond(g) = g .> mean(g)
result = by(data, :x, rows-> rows[groupcond(rows[:y]),[n for n in names(rows) if n != :x]])
@test result == DataFrame(x = [:a,:a,:b,:c],y=[3,4,7,9],z = [13,14,17,19])

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 24, 2019, 10:54pm UTC](https://discourse.julialang.org/t/query-jl-how-to-filter-on-derived-quantities-of-a-group/23486/4 "2019-04-24T22:54:33Z")

</div>

I think a more elegant way of doing it with base DataFrames would be

```julia
by(data, :x) do d
    d[d.y .> mean(d.y), :]
end

```

or

```julia
 by(data, :x) do d
    m = mean(d.y)
    filter(x -> x.y > m, d)
end

```
