11) Sorting a Data Frame with the arrange() function.

This Blog entry is from the Loading and Shaping section in Learn R.

The Blog entries that follows are born of the dplyr package which is a collection of functions that exist for the purpose of shaping and molding data frames.  The first step is to ensure that the dplyr package is available by installing it through the Install section of the packages pane.  Search for dplyr:

1.png

Click Install to download and install the dplyr package:

2.png

Load the dplyr library by typing:

library(dplyr)
3.png

The package dplyr exposes several functions for shaping and moulding data.  The arrange() function is used to rearrange, rather sort, the order of data in a data frame by columns in ascending order:

To arrange data by date for the AAPL data frame:

AAPL <- arrange(AAPL,Interim_Buffer_Date)
4.png

Run the line of script to console:

5.png

View the AAPL data frame to observe the change in row arrangement:

View(AAPL)
6.png

Run the line of script to console:

7.png

Run sort in a different direction can be achieved using the desc() function wrapped around the column to be sorted.   To change the direction of sort order on the Interim_Buffer_Date type:

AAPL <- arrange(AAPL,desc(Interim_Buffer_Date))
8.png

Run the line of script to console:

9.png

Observe the change in sort order:

View(AAPL)
10.png

Run the line of script to console:

11.png

It can be seen that the sort order has changed direction completely.  To sort by one column, then the next, simply list out the columns in order then direction of the sort:

AAPL <- arrange(AAPL,desc(Interim_Buffer_Date),Interim_Close)
12.png

Run the line of script to console:

13.png