I am trying to explore the capabilities of the DataFrames.jl module. I ran into the following issue when trying to pass the same column into a multiple input variable in the by() function.
My base example is :
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 │ a │ -0.0111914 │
│ 2 │ b │ -0.0184773 │
Now lets assume that I am not necessarily working with cov() and that I would like to pass the same column x as both the x and y inputs of that function. In the case of cov() , it is a trivial one, but trying to be as generic as possible.
I have tried the following two possibilities
by(df, :grp,result= (:x) => ((x, y),) -> cov(x, y))
Which gives the following error message :
ERROR: MethodError: no method matching cov(::Float64, ::Float64)
The error in this case is expected and I imagine that method refers to the data provided to the function object
I have also tried the following
by(df, :grp,result= (:x,:x) => ((x, y),) -> cov(x, y))
Which gives the following error message
ERROR: ArgumentError: Elements of Symbol[:x, :x] must be unique
This time I understand the error message, but I don't understand why Symbol must be unique. I have checked ?Symbol but couldn't find more details on why or how to bypass this issue (limitation?).
Effectively this prevents me from passing the same column programatically for both inputs.
So what would be the way to address this issue and be able to pass the same column twice for a function with f(x,y) ?
PS: Ahead of questions or comments on in this case (x)->cov(x,x) will work. I am aware that it will. But lets say I have a function that will compute the cov() or other functions for the (selected) columns of a dataframe I would prefer not to have to handle special cases for the diagonal items.