How does @orderby work with multiple columns?

Hello everyone,

I’m a new Julia user coming from an R/dplyr background.

I’m going through the “Tutorial for coming from dplyr” on the DataFramesMeta.jl documentation site, specifically the @orderby section using Chain.jl.

I modified one of the examples to get a feel for things. I was expecting the column :sleep_total to be in reverse order for a particular :order, but this isn’t what I’m seeing here:

julia> @chain msleep begin
           @select :name :order :sleep_total
           @orderby begin
               :order
               sortperm(:sleep_total, rev=true)
           end
       end
83×3 DataFrame
 Row │ name                            order         sleep_total
     │ String31                        String15      Float64
─────┼───────────────────────────────────────────────────────────
   1 │ Tenrec                          Afrosoricida         15.6
   2 │ Pig                             Artiodactyla          9.1
   3 │ Cow                             Artiodactyla          4.0
   4 │ Roe deer                        Artiodactyla          3.0
   5 │ Sheep                           Artiodactyla          3.8
   6 │ Goat                            Artiodactyla          5.3
   7 │ Giraffe                         Artiodactyla          1.9
   8 │ Northern fur seal               Carnivora             8.7
   9 │ Tiger                           Carnivora            15.8
  10 │ Lion                            Carnivora            13.5
  11 │ Genet                           Carnivora             6.3
  12 │ Domestic cat                    Carnivora            12.5
  13 │ Red fox                         Carnivora             9.8
  14 │ Arctic fox                      Carnivora            12.5
  15 │ Cheetah                         Carnivora            12.1
  16 │ Caspian seal                    Carnivora             3.5
  17 │ Jaguar                          Carnivora            10.4
  18 │ Gray seal                       Carnivora             6.2

For example, the :order “Artiodactyla” has its :sleep_total values in no particular order, neither ascending nor decending. I tried a couple syntax variants after looking at @orderby documentation, but I get the same result.

I hope I’m not missing anything obvious…

Thank you :slight_smile:

2 Likes

The example in the docsting is misleading. You should not use sortperm in such situations as it does not make much sense IMHO. Instead use e.g. ordinalrank from StatsBase.jl or just negation in your case (as your column is continuous):

using StatsBase
@chain msleep begin
    @select :name :order :sleep_total
    @orderby begin
        :order
        ordinalrank(:sleep_total, rev=true)
    end
end
@chain msleep begin
    @select :name :order :sleep_total
    @orderby :order -:sleep_total
end
2 Likes

I feel a bit silly for not trying -:sleep_total, but both examples produce the output I was expecting.

Thank you!

1 Like

Apologies for this. We will get this fixed soon.

1 Like