NegativeJo
NegativeJo

Reputation: 158

Julia - dataframe - How to use string for custom output column naming in by()

I am getting familiar with Julia Dataframes module. One thing that I haven't found a way yet to do is how to assign programmatically a custom column name of the result of a by() operation.

So for example I have no problem doing the following :

df = DataFrame(grp = rand(["a","b"], 100), x= rand(100), y = rand(100), z=rand(100))
by(df, :grp,result=(:x, :z)=>((x, y),) -> cov(x, y))

Giving the following dataframe

2×2 DataFrame
│ Row │ grp    │ result      │
│     │ String │ Float64     │
├─────┼────────┼─────────────┤
│ 1   │ b      │ -0.00622699 │
│ 2   │ a      │ -0.0303828  │

Now I would like the naming of the result to be dependent on some other part of my code. So i am trying things along this

resultColName="resultBis"
by(df, :grp,resultColName=(:x, :z)=>((x, y),) -> cov(x, y))

which gives me the following

2×2 DataFrame
│ Row │ grp    │ resultColName │
│     │ String │ Float64       │
├─────┼────────┼───────────────┤
│ 1   │ b      │ -0.00622699   │
│ 2   │ a      │ -0.0303828    │

Which doesn't work as I want the result column to be named 'resultBis'. I understand why this happens, but is there currently a way to provide a string to chose the custom name of the result column ?

I imagine using macro could be one way to handle that and I would welcome that as an answer, but ideally I would like to do it directly within the DataFrames.jl. Any help is welcome. Thanks

Upvotes: 2

Views: 345

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42244

julia> by(df, :grp, (; Symbol(resultColName)=>(:x, :z)=>((x, y),) -> cov(x, y)))
2×2 DataFrame
│ Row │ grp    │ resultBis  │
│     │ String │ Float64    │
├─────┼────────┼────────────┤
│ 1   │ a      │ -0.0110717 │
│ 2   │ b      │ 0.0102181  │

Explanation:

by accepts a NamedTuple as the third parameter. In order to create it programmatically we use the (; :key => value) operator. For more information type ?NamedTuple into the Julia console.

Upvotes: 3

Related Questions