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

Importing Excel data using R: Step by Step

Update: Please refer to this blog for various other ways to import excel http://www.milanor.net/blog/?p=779 as the steps outlined by me has dependency on Perl.

In this blog I’m going to share with you the steps involved in importing the data and understanding its aspects from R.

Prerequisites:

  1. Windows XP
  2. Completed the installation of R (In this example I’m using R version 2.15.2)

Step 1: Keep the Excel ready with you, in this example I’ve prepared my own sample data which is the table which captures of the various employees who have shown interest for stream change. The column labeled “Agreed” captures whether they are agreed or not. Snapshot of the worksheet:

Step 2: I have named the file as datasource.xls

Step 3: For using the Excel we will need the “gdata” package. If we have not installed it we can do it by using the command install.packages(“gdata”). Make sure you have internet connection to download the package.


Step 4: Then issue the command library(gdata), which will enable support for using .xls files in “R”


Step 5: We are going to use the command read.xls. If you need more or additional help regarding the same you can issue help (read.xls) which will start the server and load its relevant content.

Step 6: I have saved the datasource.xls in mydocuments folder

Step 7: Issue the command mydata=read.xls(“datasource.xls”) and again type mydata to see the excel file loaded to the R environment. If the file is missing or there is problem in the path you will get an error.

Step 8: In this final step we can see the summary of data using the command Summary(mydata)