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
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.
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.
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:
We can rank the same in descending order using the minus “-” symbol and changing the command as follows:
Next we will store this ordered data into OrderedScores objects for further ranking using the following command:
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.
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:
- To access the column we need to use the tableobject$columnname
- To use the Max function to get the maximum value in a vector.
- We can order the data using order function.
- We can rank the ordered data using the order function.