Skip to Content
The R Book
book

The R Book

by Michael J. Crawley
June 2007
Beginner to intermediate
950 pages
27h 8m
English
Wiley
Content preview from The R Book

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

The R Book, 2nd Edition

The R Book, 2nd Edition

Michael J. Crawley
The R Book, 3rd Edition

The R Book, 3rd Edition

Elinor Jones, Simon Harden, Michael J. Crawley
R Data Analysis Cookbook, Second Edition - Second Edition

R Data Analysis Cookbook, Second Edition - Second Edition

Kuntal Ganguly, Davor Lozić, Mzabalazo Z. Ngwenya, Andrew Bauman, Shanthi Viswanathan, Viswa Viswanathan

Publisher Resources

ISBN: 9780470510247Purchase book