Index of max values of N columns in a DataFrame without manually entering each column

The following is one way:

julia> maximum(Matrix(select(df,r"id")); dims=2)
5×1 Matrix{Int64}:
 71
 24
 34
 76
 90

and now sortperm of this vector will give the desired order (with rev).

Another way is using:

julia> select(df, r"id" => ByRow(max) => :maxid)

which gives the above vector and:

julia> df[sortperm(select(df, r"id" => ByRow(max) => :maxid); rev=true),:]
5×4 DataFrame
 Row │ idA    idB    C      idD   
     │ Int64  Int64  Int64  Int64 
─────┼────────────────────────────
   1 │    90    -64     89     -8
   2 │   -80     76    -37    -58
   3 │    71    -50     99    -99
   4 │    34    -12      6    -76
   5 │    24    -88    -69    -91

does the reordering.

This also works:

julia> select(df, AsTable(r"id") => (t->maximum(t)) => :maxid)
5×1 DataFrame
 Row │ maxid 
     │ Int64 
─────┼───────
   1 │    71
   2 │    24
   3 │    34
   4 │   -80
   5 │    90

UPDATE: The following contained an error originally, and used maximum instead of max. as parameter to orderby. The fixed statement is:

Finally, using DataFramesMeta, it is rather compact:

julia> using DataFramesMeta

julia> @orderby df -max.(AsTable(r"id")...)
5×4 DataFrame
 Row │ idA    idB    C      idD   
     │ Int64  Int64  Int64  Int64 
─────┼────────────────────────────
   1 │    90    -64     89     -8
   2 │    71    -50     99    -99
   3 │    34    -12      6    -76
   4 │    24    -88    -69    -91
   5 │   -80     76    -37    -58
2 Likes