# Combine function not naming column as cols =\> function =\> target\_cols implies

**URL:** https://discourse.julialang.org/t/combine-function-not-naming-column-as-cols-function-target-cols-implies/114831
**Category:** New to Julia
**Tags:** question, dataframes
**Created:** [May 28, 2024, 11:52am UTC](https://discourse.julialang.org/t/combine-function-not-naming-column-as-cols-function-target-cols-implies/114831 "2024-05-28T11:52:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![rhoxx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rhoxx/32/205427_2.png) [@rhoxx](https://discourse.julialang.org/u/rhoxx)
#### Post date: [May 28, 2024, 11:52am UTC](https://discourse.julialang.org/t/combine-function-not-naming-column-as-cols-function-target-cols-implies/114831/1 "2024-05-28T11:52:21Z")

</div>

Hi there I’m still new to Julia. Trying to figure out why some behavior is not as I would it expect to be.  
In the dataframes example for the split-apply-combine strategy here [https://dataframes.juliadata.org/stable/man/split\_apply\_combine/](https://dataframes.juliadata.org/stable/man/split_apply_combine/)  
there is an example:

> using DataFrames, CSV, Statistics
> 
> path = joinpath(pkgdir(DataFrames), “docs”, “src”, “assets”, “iris.csv”);
> 
> iris = CSV.read(path, DataFrame)
> 
> iris\_gdf = groupby(iris, :Species)
> 
> combine(iris\_gdf, :PetalLength =\> mean =\> :myMean)

which works fine and as intended, but when I use an anonymous function like this

> combine(iris\_gdf, :PetalLength =\> x → mean(x) =\> :myMean)

I get a data frame like this:  
 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/f/a/fa1621a62d9689a6747cbc82c9b457cbf92716b2.png)  
Which seems like a bug.  
Question is, how do I get the new column for a custom function with a proper name and proper content without renaming the column in an extra line.  
My “bad” solution is like this:

> rename(combine(iris\_gdf, :PetalLength =\> x → mean(x)), “PetalLength\_function” =\> “myMean”)

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 28, 2024, 12:09pm UTC](https://discourse.julialang.org/t/combine-function-not-naming-column-as-cols-function-target-cols-implies/114831/2 "2024-05-28T12:09:55Z")

</div>

It’s not a bug, just operator precedence, you need to enclose your anonymous function in brackets.

---

<div class="post-metadata">

### Author: ![rhoxx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rhoxx/32/205427_2.png) [@rhoxx](https://discourse.julialang.org/u/rhoxx)
#### Post date: [May 28, 2024, 12:51pm UTC](https://discourse.julialang.org/t/combine-function-not-naming-column-as-cols-function-target-cols-implies/114831/3 "2024-05-28T12:51:46Z")

</div>

you mean parenthesis?  
I got it working with

```julia
combine(iris_gdf, :PetalLength => (x -> mean(x)) => :myMean)

```

Could you please explain why

```julia
combine(iris_gdf, :PetalLength => x -> mean(x) => :myMean)

```

is different from

```julia
combine(iris_gdf, :PetalLength => (x -> mean(x)) => :myMean)

```

but

```julia
combine(iris_gdf, :PetalLength => mean => :myMean)

```

and

```julia
combine(iris_gdf, :PetalLength => (mean) => :myMean)

```

is the same?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 28, 2024, 1:33pm UTC](https://discourse.julialang.org/t/combine-function-not-naming-column-as-cols-function-target-cols-implies/114831/4 "2024-05-28T13:33:23Z")

</div>

Like I said it’s just operator precedence:

```julia
julia> :x => mean => :x
:x => (Statistics.mean => :x)

julia> :x => x -> mean(x) => :x
:x => var"#3#4"()

julia> :x => (x -> mean(x)) => :x
:x => (var"#5#6"() => :x)

```

`(mean)` is just the same as `mean`, while `x -> mean => :x` is not the same as `(x -> mean(x)) => :x` in the same way that (1) is the same as 1 but 1 + 2 \times 3 is not the same as (1 + 2) \times 3

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [May 28, 2024, 2:53pm UTC](https://discourse.julialang.org/t/combine-function-not-naming-column-as-cols-function-target-cols-implies/114831/5 "2024-05-28T14:53:44Z")

</div>

The `Pair`(s) are resolved before being passed to the `combine` function. Let’s look below at what the `combine` function is receiving as its second argument.

Your original version is a single `Pair` from the column name to a function output. (The “var” thing is denoting an anonymous function.) The output of the anonymous function is a `Pair`, which you can see in your provided `PetalLength_function` column screenshot.

```julia-repl
julia> :PetalLength => x -> mean(x) => :myMean
:PetalLength => var"#1#2"()

```

Julia reads the above the same way as if you would have put the parentheses around the final `Pair`. Written this way, you have only provided `combine` with a source column and an operation function.

```julia-repl
julia> :PetalLength => x -> (mean(x) => :myMean)
:PetalLength => var"#13#14"()

julia> typeof(:PetalLength => x -> (mean(x) => :myMean))
Pair{Symbol, var"#17#18"}

```

You need to separate the operation function from the new column name with parentheses so that `combine` sees nested `Pair`s as its second argument.

```julia-repl
julia> :PetalLength => (x -> mean(x)) => :myMean
:PetalLength => (var"#3#4"() => :myMean)

julia> typeof(:PetalLength => (x -> mean(x)) => :myMean)
Pair{Symbol, Pair{var"#5#6", Symbol}}

```

* * *

Read here to learn how to format your code snippets:

> [@PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530):
>
> This is a short post on how to use backticks (` and ```) to quote code, so it is easy to read. This post can be linked to new users who are confused by this feature. Why By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This makes it easier to read your code and help you. Displayed code looks like this: function displayed\_code(x::Int, y::Int) if x \< y println("x is smaller") else println("or not") end end Inline…

---

<div class="post-metadata">

### Author: ![rhoxx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rhoxx/32/205427_2.png) [@rhoxx](https://discourse.julialang.org/u/rhoxx)
#### Post date: [May 28, 2024, 3:44pm UTC](https://discourse.julialang.org/t/combine-function-not-naming-column-as-cols-function-target-cols-implies/114831/6 "2024-05-28T15:44:01Z")

</div>

Thank you for your explanation.  
Though, I wonder why

```julia
combine(iris_gdf, :PetalLength => mean => :myMean)

```

works as it does.

---

<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: [May 28, 2024, 3:50pm UTC](https://discourse.julialang.org/t/combine-function-not-naming-column-as-cols-function-target-cols-implies/114831/7 "2024-05-28T15:50:30Z")

</div>

In DataFramesMeta.jl (a package which I maintain), it is straightforward to use column names programatically

```julia
incol = :PetalLength
outcol = :myMean
@combine iris_gdf $outcol = mean($incol)

```

or even

```julia
@by iris :Species $outcol = mean($incol)

```

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [May 28, 2024, 3:50pm UTC](https://discourse.julialang.org/t/combine-function-not-naming-column-as-cols-function-target-cols-implies/114831/8 "2024-05-28T15:50:32Z")

</div>

> [@rhoxx](#):
>
> `combine(iris_gdf, :PetalLength => mean => :myMean)`

There is no `->` character in that expression. It is an issue of parsing order between the `=>` and `->` symbols.

As `nilshg` said, think of it like `2 * 3 + 4 * 5` vs. `2 * 3 * 5`. Then with parenthesis, the first one changes meaning `2 * (3 + 4) * 5`, but the second one doesn’t `2 * (3) * 5`.
