Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

R language can also be used for commercial applications. In this article, I will describe how R language can be used for a commercial application like Payroll. In this exercise let us assume that in a typical Indian Company, every employee gets two types of allowances called Dearness Allowance(DA) and House Rent Allowance(HRA) besides the basic Salary. Let us assume that the DA and HRA are 22% and 30% of Basic Salary respectively. In addition, the company also deducts IncomeTax, which is 10% of Basic Salary. The Gross Salary is obtained by adding the Basic Salary, DA and HRA. Net Salary is obtained by subtracting the IncomeTax amount from the Gross Salary. It is required to do all the calculations for each employee and generate the Payslips for each employee in a specified format mentioning  all these details.

Sample Data

Some sample data of four employees is shown below : Calculations and Printing of Payslips

The following User Defined Function payroll()  will perform all the calculations and returns the results  as list  of class “payroll”  :

payroll<-function(df) {
lst<-list()
lst$eno<-df$eno
lst$name<-df$name
lst$dept<-df$dept
lst$salary<-df$salary
lst$da<-df$salary*0.22
lst$hra<-df$salary*0.3
lst$gpay<-df$salary+lst$da+lst$hra
lst$itax<-df$salary*0.1
lst$npay<-lst$gpay-lst\$itax
class(lst)<-“payroll”
lst
}

In order to print the payslips the print function of payroll is defined as follows  :

print.payroll<-function(x) {
cat(“\n”)
for(i in 1:n) {
cat(“Eno : “,x[][i],”\n”)
cat(“Name : “,x[][i],”\n”)
cat(“Dept : “,x[][i],”\n”)
cat(“Salary : “,x[][i],”\n”)
cat(“DA : “,x[][i],”\n”)
cat(“HRA : “,x[][i],”\n”)
cat(“Gross Pay : “,x[][i],”\n”)
cat(“ITax : “,x[][i],”\n”)
cat(“Net Salary : “,x[][i],”\n\n”)
for(i in 1:40) {
cat(“=”)
}
cat(“\n”)
}
}

The following  R code will  read the data from the excel data file payroll.csv,  do all the calculations and  prints the payslips for all the employees.

stringsAsFactors=FALSE)
n<-nrow(df)
res<-payroll(df)
res

Payslips printing

The payslips generated by the above code is given below :  Conclusions

R language program for developing  a commercial application like Payroll is described in this article. 