# From a DataFrame, how do I create multiple new columns that each take multiple columns as input in a set name pattern?

**URL:** <https://discourse.julialang.org/t/from-a-dataframe-how-do-i-create-multiple-new-columns-that-each-take-multiple-columns-as-input-in-a-set-name-pattern/119245>\
**Category:** Data\
**Created:** [September 10, 2024, 10:30am UTC](https://discourse.julialang.org/t/from-a-dataframe-how-do-i-create-multiple-new-columns-that-each-take-multiple-columns-as-input-in-a-set-name-pattern/119245 "2024-09-10T10:30:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 10, 2024, 10:30am UTC](https://discourse.julialang.org/t/from-a-dataframe-how-do-i-create-multiple-new-columns-that-each-take-multiple-columns-as-input-in-a-set-name-pattern/119245/1 "2024-09-10T10:30:16Z")

</div>

There’s a similar question about R on [here](https://stackoverflow.com/questions/78953190/is-there-a-multiple-column-as-input-version-of-dplyrs-across).

However, I can’t find the solution in Julia.

Imagine I have a DataFrame

```julia
using DataFrames

df = DataFrame(
  group = rand(1:3, 100), 
  a1 = 1:100, a2=1:100, a3=1:100, 
  b1 = 1:100, b2=1:100, b3=1:100)

```

And I want to create new columns `ci` for `i` in 1 to 3 and they are based on the `ai` and `bi` for each i respectively.

```julia
using TidierData
using Chain: @chain
df = @chain df begin
 @group_by group
 @summarize(c1 = fn(a1, b1), c2 = fn(a2, b2), c3 = fn(a3, b3))
end

```

It’s ok to write it out for just 3 such `c`s but what if there are hundreds with more complex rules for finding the right variables?

I was hoping to be able to do something like this

```julia
using TidierData
using Chain: @chain
df = @chain df begin
 @group_by group
 @summarize
    assign("c" .* String.(1:3), fn(@starts_with("a") |> sort, @starts_with("b") |> sort))
  )
end

```

What’s the most elegant way to do this in Julia? The only way I can think of is via a macro expand but was hoping for one where I don’t have to roll my own macro.

**edit** made it into a group by example instead of just mutate

---

<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:** [September 10, 2024, 10:34am UTC](https://discourse.julialang.org/t/from-a-dataframe-how-do-i-create-multiple-new-columns-that-each-take-multiple-columns-as-input-in-a-set-name-pattern/119245/2 "2024-09-10T10:34:07Z")

</div>

Your first example can just be a loop?

```julia
for i in 1:100
    df[!, "c$i"] = fn(df[!, "a$i"], df[!, "b$i"])
end

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 10, 2024, 10:36am UTC](https://discourse.julialang.org/t/from-a-dataframe-how-do-i-create-multiple-new-columns-that-each-take-multiple-columns-as-input-in-a-set-name-pattern/119245/3 "2024-09-10T10:36:15Z")

</div>

Bad example by me, I actually need it inside a group-by. Let me change the example

---

<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:** [September 10, 2024, 12:37pm UTC](https://discourse.julialang.org/t/from-a-dataframe-how-do-i-create-multiple-new-columns-that-each-take-multiple-columns-as-input-in-a-set-name-pattern/119245/4 "2024-09-10T12:37:32Z")

</div>

inside combine you can write

`combine(gdf, [["a$i", "b$i"] => fn => "c$i" for i in 1:100])`
