# Apply weights in JuliaDB groupby

**URL:** <https://discourse.julialang.org/t/apply-weights-in-juliadb-groupby/10817>\
**Category:** Data\
**Created:** [May 10, 2018, 3:48pm UTC](https://discourse.julialang.org/t/apply-weights-in-juliadb-groupby/10817 "2018-05-10T15:48:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmarg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmarg/32/936_2.png) [@pmarg](https://discourse.julialang.org/u/pmarg)\
**Post date:** [May 10, 2018, 3:48pm UTC](https://discourse.julialang.org/t/apply-weights-in-juliadb-groupby/10817/1 "2018-05-10T15:48:00Z")

</div>

Hi,

I’ve been struggling to find a way to calculate the weighted descriptive statistics in JuliaDB using the groupby function. Assuming that “w” is a column of the table DB with sampling weights, I would like to find something like this:

```julia
groupby(@NT(avg=z->mean(z, weights(:w)), DB, (:x,:y), select=:z)

```

I understand why the above doesn’t work, but I cannot come up with a way to pass the weights w in the mean (or any other) function.

Thanks!

---

<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:** [May 10, 2018, 4:24pm UTC](https://discourse.julialang.org/t/apply-weights-in-juliadb-groupby/10817/2 "2018-05-10T16:24:13Z")

</div>

You need to select both columns in the `groupby`, meaning:

`groupby(@NT(avg=z->mean(column(z, :z), weights(column(z,:w))), DB, (:x,:y), select=(:z, :w))`

To manipulate symbols as columns as you were doing you’d need some sort of metaprogramming, for example:

```julia
using JuliaDBMeta
@groupby DB (:x, :y) @NT(avg = mean(:z, weights(:w))))

```

---

<div class="post-metadata">

**Author:** ![pmarg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmarg/32/936_2.png) [@pmarg](https://discourse.julialang.org/u/pmarg)\
**Post date:** [May 10, 2018, 5:07pm UTC](https://discourse.julialang.org/t/apply-weights-in-juliadb-groupby/10817/3 "2018-05-10T17:07:38Z")

</div>

Thank you very much, that worked!

Could you include an example like the above in your JuliaDB tutorial for others to see? I found it extremely helpful and I believe this should be a common question for the ones that work with survey data.

I see that there is an example that you call a column but it wasn’t clear to me that it has general applications.

For example, in the above, I was trying:

```julia
groupby(@NT(avg=p->mean(p.z, weights(p.w)), DB, (:x,:y), select=(:z,:w))

```

because I was mimicking the filter function.

---

<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:** [May 10, 2018, 5:33pm UTC](https://discourse.julialang.org/t/apply-weights-in-juliadb-groupby/10817/4 "2018-05-10T17:33:12Z")

</div>

Good point, I’ll go through the tutorial again shortly as we decided to add it to the official docs, I’ll definitely keep in mind that grouping when needing two columns is a bit tricky so I’ll add an example about that. The “inconvience” is that `t.x` works on a row to select a field, but not on the table to select a column, and you need `column(t, :x)`. Though in Julia 0.7 we could probably use dot overloading and allow `t.x` to select a column (if there’s consensus to do that).
