# How to specify a transformation with multiple arguments via minilanguage

**URL:** <https://discourse.julialang.org/t/how-to-specify-a-transformation-with-multiple-arguments-via-minilanguage/89474>\
**Category:** General Usage\
**Tags:** question, dataframes\
**Created:** [October 29, 2022, 4:45am UTC](https://discourse.julialang.org/t/how-to-specify-a-transformation-with-multiple-arguments-via-minilanguage/89474 "2022-10-29T04:45:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [October 29, 2022, 4:45am UTC](https://discourse.julialang.org/t/how-to-specify-a-transformation-with-multiple-arguments-via-minilanguage/89474/1 "2022-10-29T04:45:41Z")

</div>

Suppose I have a DataFrame df and GroupDataFrame

```julia
df = DataFrame(a=[1,1,2,2], b =[3,3,4,4])
dfg = groupby(df,:a)

```

further suppose I have a function `fun1` that takes a vector x and multiple arguments y, z.

```julia
function fun1(x,y,z)
       for i = eachindex(x)
       x[i] = (x[i]+y)^z
       end
       end

```

Is there a way to pass the `fun1` transformation via the  
`source_column => transformation => target_column_name` format? I tried using an anonymous function in parenthesis but it yielded

```julia
 transform!(dfg, :a => (x -> fun1(x,1,2)) => :c)
4×3 DataFrame
 Row │ a b c       
     │ Int64 Int64 Nothing 
─────┼───────────────────────
   1 │ 4 3          
   2 │ 4 3          
   3 │ 9 4          
   4 │ 9 4     

```

as opposed to the desired result of

```julia
 Row │ a b c       
     │ Int64 Int64 Nothing 
─────┼───────────────────────
   1 │ 1 3 4   
   2 │ 1 3 4    
   3 │ 2 4 9    
   4 │ 2 4 9

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 29, 2022, 6:31am UTC](https://discourse.julialang.org/t/how-to-specify-a-transformation-with-multiple-arguments-via-minilanguage/89474/2 "2022-10-29T06:31:24Z")

</div>

The problem, I think, is the language, not the mini-language.  
In the fun1 function there are two things to change:

1. “export” the result of the calculation;
2. avoid that the input vector is also modified

PS  
on the second point, others, better than I can, could explain what happens and how to act in this case

```julia

function fun1(x,y,z)
    out=similar(x)
    for i = eachindex(x)
        out[i] = (x[i]+y)^z
    end
    out
end

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [October 29, 2022, 6:36am UTC](https://discourse.julialang.org/t/how-to-specify-a-transformation-with-multiple-arguments-via-minilanguage/89474/3 "2022-10-29T06:36:29Z")

</div>

Yes, @rocco_sprmnt21 is correct in both points. It would be simplest to just write:

```julia
fun1(x,y,z) = (x .+ y) .^ z

```

and all would be good.

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [October 31, 2022, 8:43am UTC](https://discourse.julialang.org/t/how-to-specify-a-transformation-with-multiple-arguments-via-minilanguage/89474/4 "2022-10-31T08:43:45Z")

</div>

Thanks so much this clears up a lot! I think that with the simple `fun1`, `transform!` can easily be modified to acommodate a `y` that is in a different column but the same row of `dfg` with something like

```julia
transform!(dfg, [:a, :b] => ((x,y) -> fun1(x,y,2)) => :newcol)

```

Which yields `fun1(dfg[k].a[j], dfg[k].b[j],2)` into `newcol`.

But what if y or z is drawn from a different row of the DataFrame, e.g. if the desired output for `newcol` is `fun1( dfg[k].a[j], dfg[k].b[j+1], 2 )`. Can `transform` with the `minilaguage` be modified to accommodate operations on different rows or in this case is it better to just modify `fun1` or shift `col b`?

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [October 31, 2022, 9:37am UTC](https://discourse.julialang.org/t/how-to-specify-a-transformation-with-multiple-arguments-via-minilanguage/89474/5 "2022-10-31T09:37:19Z")

</div>

Thanks! Just curious could you explain the distinction between “exporting” the calculation with just

```julia
    end
    out
end

```

verses

```julia
    end 
    return out
end 

```

Is the `return` not necessary here?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 31, 2022, 2:59pm UTC](https://discourse.julialang.org/t/how-to-specify-a-transformation-with-multiple-arguments-via-minilanguage/89474/6 "2022-10-31T14:59:20Z")

</div>

Yes, in this case the two forme are equivalent.
