3.2.8 Sorting Data

The ore.sort function enables flexible sorting of a data frame along one or more columns specified by the by argument.

The ore.sort function can be used with other data pre-processing functions. The results of sorting can provide input to R visualization.

The sorting done by the ore.sort function takes place in the Oracle database. The ore.sort function supports the database nls.sort option.

The ore.sort function returns an ore.frame.

For details about the function arguments, invoke help(ore.sort).

Most of the following examples use the NARROW data set. Some examples use the ONTIME_S data set.

Example 3-46 Sorting Columns in Descending Order

This example sorts the columns AGE and GENDER in descending order.

x <- ore.sort(data=NARROW, by='AGE,GENDER', reverse=TRUE)

Example 3-47 Sorting Different Columns in Different Orders

This example sorts AGE in descending order and GENDER in ascending order.

x <- ore.sort(data=NARROW, by='-AGE,GENDER')

Example 3-48 Sorting and Returning One Row per Unique Value

This example sorts by AGE and keep one row per unique value of AGE:

x <- ore.sort(data=NARROW, by='AGE', unique.key=TRUE)

Example 3-49 Removing Duplicate Columns

This example sorts by AGE and removes duplicate rows:

x <- ore.sort(data=NARROW, by='AGE', unique.data=TRUE)

Example 3-50 Removing Duplicate Columns and Returning One Row per Unique Value

This example sorts by AGE, removes duplicate rows, and returns one row per unique value of AGE.

x <- ore.sort(data=NARROW, by='AGE', unique.data=TRUE, unique.key = TRUE)

Example 3-51 Preserving Relative Order in the Output

This example maintains the relative order in the sorted output.

x <- ore.sort(data=NARROW, by='AGE', stable=TRUE)

Example 3-52 Sorting Two Columns in Different Orders

This example sorts ONTIME_S by airline name in descending order and departure delay in ascending order.

sortedOnTime1 <- ore.sort(data=ONTIME_S, by='-UNIQUECARRIER,DEPDELAY')

Example 3-53 Sorting Two Columns in Different Orders and Producing Unique Combinations

This example sorts ONTIME_S by airline name and departure delay and selects one of each combination (that is, returns a unique key).

sortedOnTime1 <- ore.sort(data=ONTIME_S, by='-UNIQUECARRIER,DEPDELAY', 
                          unique.key=TRUE)