# How to reference new variables in a combine for dataframes?

**URL:** <https://discourse.julialang.org/t/how-to-reference-new-variables-in-a-combine-for-dataframes/52851>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [January 4, 2021, 9:41pm UTC](https://discourse.julialang.org/t/how-to-reference-new-variables-in-a-combine-for-dataframes/52851 "2021-01-04T21:41:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdeonovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdeonovic/32/3928_2.png) [@bdeonovic](https://discourse.julialang.org/u/bdeonovic)\
**Post date:** [January 4, 2021, 9:41pm UTC](https://discourse.julialang.org/t/how-to-reference-new-variables-in-a-combine-for-dataframes/52851/1 "2021-01-04T21:41:45Z")

</div>

Posted this on slack, but I think I’ll move over to using discourse more (I’ll post a cross link)

when doing split-apply-combine with a DataFrame is there anyway for the apply transformations to reference previously calculated variables?

```julia
df = DataFrame(a=repeat([1,2],inner=[5]), b='A':'J', c=rand(10))
gdf = groupby(df, :a)
combine(gdf, nrow => :n, :c => mean => :m, [:n, :m] => ((n,m)->n-m) => :y) #doesn't work :n and :m are not visible here

```

---

<div class="post-metadata">

**Author:** ![bdeonovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdeonovic/32/3928_2.png) [@bdeonovic](https://discourse.julialang.org/u/bdeonovic)\
**Post date:** [January 4, 2021, 9:54pm UTC](https://discourse.julialang.org/t/how-to-reference-new-variables-in-a-combine-for-dataframes/52851/2 "2021-01-04T21:54:02Z")

</div>

Using `Query.jl` I can do this, but i think its quite verbose…any suggestions?

```julia
df |> @groupby({_.a}) |> 
@map({a=key(_)[1], n=length(_), m=mean(_.c)}) |> 
@map({a=_.a, n=_.n, m=_.m, y=_.n-_.m})

```

---

<div class="post-metadata">

**Author:** ![bdeonovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdeonovic/32/3928_2.png) [@bdeonovic](https://discourse.julialang.org/u/bdeonovic)\
**Post date:** [January 4, 2021, 10:23pm UTC](https://discourse.julialang.org/t/how-to-reference-new-variables-in-a-combine-for-dataframes/52851/3 "2021-01-04T22:23:33Z")

</div>

Maybe this one generalizes a bit better

```julia
@from i in df begin
    @group i by i.a into g
    @let n=length(g)
    @let m=mean(g.c)
    @select {a=key(g), n=n, m=m, y=n-m}
    @collect DataFrame
end

```

---

<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:** [January 4, 2021, 10:42pm UTC](https://discourse.julialang.org/t/how-to-reference-new-variables-in-a-combine-for-dataframes/52851/4 "2021-01-04T22:42:59Z")

</div>

No this is not possible currently (I thought it was…). You are going to have to use two transform statements. Note that you can make the second one a `transform!` which will reduce the memory footprint a bit.

---

<div class="post-metadata">

**Author:** ![bdeonovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdeonovic/32/3928_2.png) [@bdeonovic](https://discourse.julialang.org/u/bdeonovic)\
**Post date:** [January 6, 2021, 8:13pm UTC](https://discourse.julialang.org/t/how-to-reference-new-variables-in-a-combine-for-dataframes/52851/5 "2021-01-06T20:13:48Z")

</div>

I ended up using the approach taken here:

> [@Dpylr do equivalent in query.jl standalone syntax?](https://discourse.julialang.org/t/dpylr-do-equivalent-in-query-jl-standalone-syntax/24745/4):
>
> Whoa cool. So what does the \_ \_ refer to on the @mapmany line? It looks like it’s the original dataframe somehow, but how does it know that that’s what you’re referring to?
