* A B C
A 1 2 3
B 2 3 4
C 3 4 5
And I want to sort descending both on columns and rows after the sum something like this:
* C B A S
C 5 4 3 12
B 4 3 2 9
A 3 2 1 6
S 12 9 6 *
All is doable, can you confirm that this is the exact way how your input data frame looks like and how your ouput should look? (then I can give you information how to transform input into output)
julia> input = DataFrame("*" => ["A", "B", "C"], "A" => 1:3, "B" => 2:4, "C" => 3:5)
3Γ4 DataFrame
Row β * A B C
β String Int64 Int64 Int64
ββββββΌβββββββββββββββββββββββββββββ
1 β A 1 2 3
2 β B 2 3 4
3 β C 3 4 5
julia> output = DataFrame("*" => ["C", "B", "A", "S"], "C" => [5:-1:3; 12], "B" => [4:-1:2; 9], "A" => [3:-1:1; 6], "S" => [12, 9, 6, "*"])
4Γ5 DataFrame
Row β * C B A S
β String Int64 Int64 Int64 Any
ββββββΌββββββββββββββββββββββββββββββββββ
1 β C 5 4 3 12
2 β B 4 3 2 9
3 β A 3 2 1 6
4 β S 12 9 6 *
Yes the my problem has a table of over 1000x1000 with labels on raws and columns and I want to have in the top left corner the values that have the highest sums of absolute values. The initial table is matrix in Julia.
Here are the steps to do (I designed them so that they should be hopefully easy to grasp - I did not want to squeeze too much into one step):
julia> input = DataFrame("*" => ["A", "B", "C"], "A" => 1:3, "B" => 2:4, "C" => 3:5)
3Γ4 DataFrame
Row β * A B C
β String Int64 Int64 Int64
ββββββΌβββββββββββββββββββββββββββββ
1 β A 1 2 3
2 β B 2 3 4
3 β C 3 4 5
julia> output = select(input, Not("*"))
3Γ3 DataFrame
Row β A B C
β Int64 Int64 Int64
ββββββΌβββββββββββββββββββββ
1 β 1 2 3
2 β 2 3 4
3 β 3 4 5
julia> select!(output, sortperm(sum.(eachcol(output)), rev=true))
3Γ3 DataFrame
Row β C B A
β Int64 Int64 Int64
ββββββΌβββββββββββββββββββββ
1 β 3 2 1
2 β 4 3 2
3 β 5 4 3
julia> output.S = sum(eachcol(output))
3-element Vector{Int64}:
6
9
12
julia> insertcols!(output, 1, "*" => input."*")
3Γ5 DataFrame
Row β * C B A S
β String Int64 Int64 Int64 Int64
ββββββΌββββββββββββββββββββββββββββββββββββ
1 β A 3 2 1 6
2 β B 4 3 2 9
3 β C 5 4 3 12
julia> sort!(output, "S", rev=true)
3Γ5 DataFrame
Row β * C B A S
β String Int64 Int64 Int64 Int64
ββββββΌββββββββββββββββββββββββββββββββββββ
1 β C 5 4 3 12
2 β B 4 3 2 9
3 β A 3 2 1 6
julia> push!(output, ["S"; sum.(eachcol(output[:, Not(1, end)])); "*"], promote=true)
4Γ5 DataFrame
Row β * C B A S
β String Int64 Int64 Int64 Any
ββββββΌββββββββββββββββββββββββββββββββββ
1 β C 5 4 3 12
2 β B 4 3 2 9
3 β A 3 2 1 6
4 β S 12 9 6 *
Probably the trickiest part is the last step with sum.(eachcol(output[:, Not(1, end)])) where you want to sum everything except first and last column, which are special and you need to manually assign values to them.