On this page:
reorder
by-vector
orderable?
orderable<?

9 Sorting🔗ℹ

These operations are related to sorting data-frames.

Note that sorting a grouped data frame without respect to groups is not possible due to the way groups function internally.

procedure

(reorder df column-spec ...)

  (or/c data-frame? grouped-data-frame?)
  df : (or/c data-frame? grouped-data-frame?)
  column-spec : (or/c string? (cons/c string? (-> any/c any/c boolean?)))
Returns df, except sorted by a series of columns. Each column-spec is either a column name, or a pair of a column name and a comparator.

If no comparator is specified, it defaults to orderable<?, which will cause an ascending sort for most types of data.

If a custom ordering is specified, it should be a strict ordering (so, if objects are equal?, the comparator should return false). Otherwise, group-with and other operations involving binary search will behave unexpectedly.

Note that reordering ensures that the first column-spec is truly sorted. The subsequent column-specs are used to break the ties (i.e. two values are equal?).

This operation sorts within groups, if there are any. If you wish to sort ignoring groups, you will have to use ungroup first. This is due to the way groups are managed internally.

Examples:
> (~> example-df
      (reorder (cons "trt" string-ci>?)
               (cons "adult" >))
      show)

data-frame: 5 rows x 4 columns

┌───┬───┬─────┬───┐

│grp│trt│adult│juv│

├───┼───┼─────┼───┤

│b  │b  │5    │50 │

├───┼───┼─────┼───┤

│b  │b  │4    │40 │

├───┼───┼─────┼───┤

│a  │b  │2    │20 │

├───┼───┼─────┼───┤

│a  │b  │1    │10 │

├───┼───┼─────┼───┤

│b  │a  │3    │30 │

└───┴───┴─────┴───┘

> (~> example-df
      (group-with "trt")
      (reorder (cons "adult" >))
      show)

data-frame: 5 rows x 4 columns

groups: (trt)

┌───┬───┬───┬─────┐

│juv│trt│grp│adult│

├───┼───┼───┼─────┤

│30 │a  │b  │3    

├───┼───┼───┼─────┤

│50 │b  │b  │5    

├───┼───┼───┼─────┤

│40 │b  │b  │4    

├───┼───┼───┼─────┤

│20 │b  │a  │2    

├───┼───┼───┼─────┤

│10 │b  │a  │1    

└───┴───┴───┴─────┘

procedure

(by-vector vec)  (-> any/c any/c boolean?)

  vec : vector?
Returns a comparator based on the positions in the given input vector vec.

This is useful for "replacing" a column in a frame, while retaining the order of observations.

Example:
> (~> example-df
      (reorder (cons "juv" (by-vector (vector 50 20 30 10 40))))
      show)

data-frame: 5 rows x 4 columns

┌───┬───┬─────┬───┐

│grp│trt│adult│juv│

├───┼───┼─────┼───┤

│b  │b  │5    │50 │

├───┼───┼─────┼───┤

│a  │b  │2    │20 │

├───┼───┼─────┼───┤

│b  │a  │3    │30 │

├───┼───┼─────┼───┤

│a  │b  │1    │10 │

├───┼───┼─────┼───┤

│b  │b  │4    │40 │

└───┴───┴─────┴───┘

procedure

(orderable? v)  boolean?

  v : any/c
Determines if the given type of value v can be sorted against another orderable? by orderable<?.

v must be one of boolean?, char?, real?, symbol?, keyword?, string?, null?, void?, or eof-object?.

This does not guarantee that orderable<? will succeed. For example, orderable<? will error if given two void?s.

procedure

(orderable<? a b)  boolean?

  a : orderable?
  b : orderable?
A generic comparator to determine if a is less than b.

Used by default by joins and reorder.