# Ranking using R: Step by step

Ranking using R: Step by Step

In this blog we are going to see how we can rank the students in a class based on their total scores using R.

Let’s have the worksheet with the details of the all the students with their marks and total. We will name the worksheet as DataSourceMarks.xls

1. Next step would be import the marks to the R environment please refer to my earlier blog on steps to import the excel data using R.

2. First let’s try to get what is the maximum total score in this table. We can do that by using the tablename accompanied by \$ with the column name. So for getting the maximum of the total score we can use the following command.

>max(studentsdata\$TotalScores)

437

3. Before we rank the data we will try to order by the data by TotalScores and Name by using the following command in the ascending order:

> studentsdata[with(studentsdata,order(TotalScores,Name)),]

4. We can rank the same in descending order using the minus “-” symbol and changing the command as follows:

5. Next we will store this ordered data into OrderedScores objects for further ranking using the following command:

> OrderedScores=studentsdata[with(studentsdata,order(-TotalScores,Name)),]

6. Next we will create new object with rank column added to this table after the ranking being done on the orderedscores using the following command.

>RankedScores=within(OrderedScores,Rank=ave(TotalScores,Name,FUN=function(x)rev(order(x))))

>RankedScores

Now we have got the ranked data where in the person named Prabhakar with the total score of 437 is ranked number 1

Things we have learnt:

1. To access the column we need to use the tableobject\$columnname
2. To use the Max function to get the maximum value in a vector.
3. We can order the data using order function.
4. We can rank the ordered data using the order function.