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[[1]][i],”\n”)
cat(“Name : “,x[[2]][i],”\n”)
cat(“Dept : “,x[[3]][i],”\n”)
cat(“Salary : “,x[[4]][i],”\n”)
cat(“DA : “,x[[5]][i],”\n”)
cat(“HRA : “,x[[6]][i],”\n”)
cat(“Gross Pay : “,x[[7]][i],”\n”)
cat(“ITax : “,x[[8]][i],”\n”)
cat(“Net Salary : “,x[[9]][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.
df<-read.csv(“g:/RExercises/OOP/payroll.csv”,header=TRUE,sep=”,”,
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.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.