Sorting, Ranking and Ordering

These three related concepts are important, and one of them (order) is difficult to understand on first acquaintance. Let's take a simple example:

houses<-read.table("c:\\temp \\houses.txt",header=T)
attach(houses)
names(houses)

[1]  "Location"  "Price"

Now we apply the three different functions to the vector called Price,

ranks<-rank(Price)
sorted<-sort(Price)
ordered<-order(Price)

and make a dataframe out of the four vectors like this:

view<-data.frame(Price,ranks,sorted,ordered)
view

      Price     ranks     sorted     ordered
1       325      12.0         95           9
2       201      10.0        101           6
3       157       5.0        117          10
4       162       6.0        121          12
5       164       7.0        157           3
6       101       2.0        162           4
7       211      11.0        164           5
8       188       8.5        188           8
9        95       1.0        188          11
10      117       3.0        201           2
11      188       8.5        211           7
12      121       4.0        325           1

Rank

The prices themselves are in no particular sequence. The ranks column contains the value that is the rank of the particular data point (value of Price), where 1 is assigned to the lowest data point and length(Price) – here 12 – is assigned to the highest data point. So the first element, Price=325, is the highest value in Price. You should check that there are 11 values smaller than 325 in the vector called Price. Fractional ranks indicate ties. There are two 188s in Price and their ranks are 8 and 9. Because they are tied, each gets the average of their two ranks (8 + 9)/2 = 8 5.

Sort

The sorted vector is very straightforward. It contains the values of Price sorted into ascending order. If you want to sort into descending order, use the ...

Get The R Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.