JuliaDB.jl
JuliaDB.jl copied to clipboard
How to perform `groupreduce` on multiple selected columns?
I want to create the max(v1) and sum(v2) grouped by id.
This is my code
groupreduce(max, mytable, :id, select = :v1)
groupreduce(+, mytable, :id, select = :v2)
but given I am grouping by the same columnn, I would actually want to have a syntax like this to apply this at once
groupreduce((max,) , (+,), mytable, :id, select = (:v1, :v2))
May be this is one of those cases where the JuliaDB syntax could be simplified a bit. You need something like:
func(i, j) = @NT(v1 = max(i.v1, j.v1), v2 = i.v2+j.v2)
groupreduce(func, mytable, :id, select = (:v1, :v2))
(you have to specify the reduction in terms of NamedTuples)
This should also work:
groupreduce((:max => :v1 => max, :sum => :v2 => +), mytable, :id, select = (:v1, :v2))
groupreduce((:max => :v1 => max, :sum => :v2 => +), mytable, :id) is the simplest command which does this.
Worth adding to the doc
what is the syntax when 1) not using named tuples 2) using NDSparse and 3) no new column
maybe? groupreduce(( 1 => max, 2 => +), mytable, (1,2,3))