Ordering (sorting) rows of a data frame by the certain variables in the data frame. This function is essentially a wrapper for the order() function - the important difference being that variables to order by can be given by a model formula.

order_by(data, formula)

orderBy(formula, data)

Arguments

data

A dataframe

formula

The right hand side of a formula

Value

The ordered data frame

Details

The sign of the terms in the formula determines whether sorting should be ascending or decreasing; see examples below

Author

Søren Højsgaard, sorenh@math.aau.dk and Kevin Wright

Examples


orderBy(~ conc + Treatment, CO2)
#>    Plant        Type  Treatment conc uptake
#> 1    Qn1      Quebec nonchilled   95   16.0
#> 8    Qn2      Quebec nonchilled   95   13.6
#> 15   Qn3      Quebec nonchilled   95   16.2
#> 43   Mn1 Mississippi nonchilled   95   10.6
#> 50   Mn2 Mississippi nonchilled   95   12.0
#> 57   Mn3 Mississippi nonchilled   95   11.3
#> 22   Qc1      Quebec    chilled   95   14.2
#> 29   Qc2      Quebec    chilled   95    9.3
#> 36   Qc3      Quebec    chilled   95   15.1
#> 64   Mc1 Mississippi    chilled   95   10.5
#> 71   Mc2 Mississippi    chilled   95    7.7
#> 78   Mc3 Mississippi    chilled   95   10.6
#> 2    Qn1      Quebec nonchilled  175   30.4
#> 9    Qn2      Quebec nonchilled  175   27.3
#> 16   Qn3      Quebec nonchilled  175   32.4
#> 44   Mn1 Mississippi nonchilled  175   19.2
#> 51   Mn2 Mississippi nonchilled  175   22.0
#> 58   Mn3 Mississippi nonchilled  175   19.4
#> 23   Qc1      Quebec    chilled  175   24.1
#> 30   Qc2      Quebec    chilled  175   27.3
#> 37   Qc3      Quebec    chilled  175   21.0
#> 65   Mc1 Mississippi    chilled  175   14.9
#> 72   Mc2 Mississippi    chilled  175   11.4
#> 79   Mc3 Mississippi    chilled  175   18.0
#> 3    Qn1      Quebec nonchilled  250   34.8
#> 10   Qn2      Quebec nonchilled  250   37.1
#> 17   Qn3      Quebec nonchilled  250   40.3
#> 45   Mn1 Mississippi nonchilled  250   26.2
#> 52   Mn2 Mississippi nonchilled  250   30.6
#> 59   Mn3 Mississippi nonchilled  250   25.8
#> 24   Qc1      Quebec    chilled  250   30.3
#> 31   Qc2      Quebec    chilled  250   35.0
#> 38   Qc3      Quebec    chilled  250   38.1
#> 66   Mc1 Mississippi    chilled  250   18.1
#> 73   Mc2 Mississippi    chilled  250   12.3
#> 80   Mc3 Mississippi    chilled  250   17.9
#> 4    Qn1      Quebec nonchilled  350   37.2
#> 11   Qn2      Quebec nonchilled  350   41.8
#> 18   Qn3      Quebec nonchilled  350   42.1
#> 46   Mn1 Mississippi nonchilled  350   30.0
#> 53   Mn2 Mississippi nonchilled  350   31.8
#> 60   Mn3 Mississippi nonchilled  350   27.9
#> 25   Qc1      Quebec    chilled  350   34.6
#> 32   Qc2      Quebec    chilled  350   38.8
#> 39   Qc3      Quebec    chilled  350   34.0
#> 67   Mc1 Mississippi    chilled  350   18.9
#> 74   Mc2 Mississippi    chilled  350   13.0
#> 81   Mc3 Mississippi    chilled  350   17.9
#> 5    Qn1      Quebec nonchilled  500   35.3
#> 12   Qn2      Quebec nonchilled  500   40.6
#> 19   Qn3      Quebec nonchilled  500   42.9
#> 47   Mn1 Mississippi nonchilled  500   30.9
#> 54   Mn2 Mississippi nonchilled  500   32.4
#> 61   Mn3 Mississippi nonchilled  500   28.5
#> 26   Qc1      Quebec    chilled  500   32.5
#> 33   Qc2      Quebec    chilled  500   38.6
#> 40   Qc3      Quebec    chilled  500   38.9
#> 68   Mc1 Mississippi    chilled  500   19.5
#> 75   Mc2 Mississippi    chilled  500   12.5
#> 82   Mc3 Mississippi    chilled  500   17.9
#> 6    Qn1      Quebec nonchilled  675   39.2
#> 13   Qn2      Quebec nonchilled  675   41.4
#> 20   Qn3      Quebec nonchilled  675   43.9
#> 48   Mn1 Mississippi nonchilled  675   32.4
#> 55   Mn2 Mississippi nonchilled  675   31.1
#> 62   Mn3 Mississippi nonchilled  675   28.1
#> 27   Qc1      Quebec    chilled  675   35.4
#> 34   Qc2      Quebec    chilled  675   37.5
#> 41   Qc3      Quebec    chilled  675   39.6
#> 69   Mc1 Mississippi    chilled  675   22.2
#> 76   Mc2 Mississippi    chilled  675   13.7
#> 83   Mc3 Mississippi    chilled  675   18.9
#> 7    Qn1      Quebec nonchilled 1000   39.7
#> 14   Qn2      Quebec nonchilled 1000   44.3
#> 21   Qn3      Quebec nonchilled 1000   45.5
#> 49   Mn1 Mississippi nonchilled 1000   35.5
#> 56   Mn2 Mississippi nonchilled 1000   31.5
#> 63   Mn3 Mississippi nonchilled 1000   27.8
#> 28   Qc1      Quebec    chilled 1000   38.7
#> 35   Qc2      Quebec    chilled 1000   42.4
#> 42   Qc3      Quebec    chilled 1000   41.4
#> 70   Mc1 Mississippi    chilled 1000   21.9
#> 77   Mc2 Mississippi    chilled 1000   14.4
#> 84   Mc3 Mississippi    chilled 1000   19.9
## Sort decreasingly by conc
orderBy(~ - conc + Treatment, CO2)
#>    Plant        Type  Treatment conc uptake
#> 7    Qn1      Quebec nonchilled 1000   39.7
#> 14   Qn2      Quebec nonchilled 1000   44.3
#> 21   Qn3      Quebec nonchilled 1000   45.5
#> 49   Mn1 Mississippi nonchilled 1000   35.5
#> 56   Mn2 Mississippi nonchilled 1000   31.5
#> 63   Mn3 Mississippi nonchilled 1000   27.8
#> 28   Qc1      Quebec    chilled 1000   38.7
#> 35   Qc2      Quebec    chilled 1000   42.4
#> 42   Qc3      Quebec    chilled 1000   41.4
#> 70   Mc1 Mississippi    chilled 1000   21.9
#> 77   Mc2 Mississippi    chilled 1000   14.4
#> 84   Mc3 Mississippi    chilled 1000   19.9
#> 6    Qn1      Quebec nonchilled  675   39.2
#> 13   Qn2      Quebec nonchilled  675   41.4
#> 20   Qn3      Quebec nonchilled  675   43.9
#> 48   Mn1 Mississippi nonchilled  675   32.4
#> 55   Mn2 Mississippi nonchilled  675   31.1
#> 62   Mn3 Mississippi nonchilled  675   28.1
#> 27   Qc1      Quebec    chilled  675   35.4
#> 34   Qc2      Quebec    chilled  675   37.5
#> 41   Qc3      Quebec    chilled  675   39.6
#> 69   Mc1 Mississippi    chilled  675   22.2
#> 76   Mc2 Mississippi    chilled  675   13.7
#> 83   Mc3 Mississippi    chilled  675   18.9
#> 5    Qn1      Quebec nonchilled  500   35.3
#> 12   Qn2      Quebec nonchilled  500   40.6
#> 19   Qn3      Quebec nonchilled  500   42.9
#> 47   Mn1 Mississippi nonchilled  500   30.9
#> 54   Mn2 Mississippi nonchilled  500   32.4
#> 61   Mn3 Mississippi nonchilled  500   28.5
#> 26   Qc1      Quebec    chilled  500   32.5
#> 33   Qc2      Quebec    chilled  500   38.6
#> 40   Qc3      Quebec    chilled  500   38.9
#> 68   Mc1 Mississippi    chilled  500   19.5
#> 75   Mc2 Mississippi    chilled  500   12.5
#> 82   Mc3 Mississippi    chilled  500   17.9
#> 4    Qn1      Quebec nonchilled  350   37.2
#> 11   Qn2      Quebec nonchilled  350   41.8
#> 18   Qn3      Quebec nonchilled  350   42.1
#> 46   Mn1 Mississippi nonchilled  350   30.0
#> 53   Mn2 Mississippi nonchilled  350   31.8
#> 60   Mn3 Mississippi nonchilled  350   27.9
#> 25   Qc1      Quebec    chilled  350   34.6
#> 32   Qc2      Quebec    chilled  350   38.8
#> 39   Qc3      Quebec    chilled  350   34.0
#> 67   Mc1 Mississippi    chilled  350   18.9
#> 74   Mc2 Mississippi    chilled  350   13.0
#> 81   Mc3 Mississippi    chilled  350   17.9
#> 3    Qn1      Quebec nonchilled  250   34.8
#> 10   Qn2      Quebec nonchilled  250   37.1
#> 17   Qn3      Quebec nonchilled  250   40.3
#> 45   Mn1 Mississippi nonchilled  250   26.2
#> 52   Mn2 Mississippi nonchilled  250   30.6
#> 59   Mn3 Mississippi nonchilled  250   25.8
#> 24   Qc1      Quebec    chilled  250   30.3
#> 31   Qc2      Quebec    chilled  250   35.0
#> 38   Qc3      Quebec    chilled  250   38.1
#> 66   Mc1 Mississippi    chilled  250   18.1
#> 73   Mc2 Mississippi    chilled  250   12.3
#> 80   Mc3 Mississippi    chilled  250   17.9
#> 2    Qn1      Quebec nonchilled  175   30.4
#> 9    Qn2      Quebec nonchilled  175   27.3
#> 16   Qn3      Quebec nonchilled  175   32.4
#> 44   Mn1 Mississippi nonchilled  175   19.2
#> 51   Mn2 Mississippi nonchilled  175   22.0
#> 58   Mn3 Mississippi nonchilled  175   19.4
#> 23   Qc1      Quebec    chilled  175   24.1
#> 30   Qc2      Quebec    chilled  175   27.3
#> 37   Qc3      Quebec    chilled  175   21.0
#> 65   Mc1 Mississippi    chilled  175   14.9
#> 72   Mc2 Mississippi    chilled  175   11.4
#> 79   Mc3 Mississippi    chilled  175   18.0
#> 1    Qn1      Quebec nonchilled   95   16.0
#> 8    Qn2      Quebec nonchilled   95   13.6
#> 15   Qn3      Quebec nonchilled   95   16.2
#> 43   Mn1 Mississippi nonchilled   95   10.6
#> 50   Mn2 Mississippi nonchilled   95   12.0
#> 57   Mn3 Mississippi nonchilled   95   11.3
#> 22   Qc1      Quebec    chilled   95   14.2
#> 29   Qc2      Quebec    chilled   95    9.3
#> 36   Qc3      Quebec    chilled   95   15.1
#> 64   Mc1 Mississippi    chilled   95   10.5
#> 71   Mc2 Mississippi    chilled   95    7.7
#> 78   Mc3 Mississippi    chilled   95   10.6