# Calculating IRR in R

Calculation of IRR in R is easy with the package called `jrvFinance`. You can install this package with the command `install.packages("jrvFinance")`.

Why do we need to calculate the IRR or Internal rate of returns?

Let us suppose a manager has an opportunity to invest in two projects, but can only choose one project. The projects are:

Project 1 requires \$800 million in investment today, but it will pay 200,250,300,350,400 million in payments each year for the next 5 years.

Project 2 required \$500 million in investment today, but it will pay 150,170,178,250,300 in payments each year for the next 5 years.

How would the manager decide which project to choose? To solve this problem we need to calculate the IRR of the two projects. We show the solution below.

``````library(jrvFinance) # This package has a the irr formula to solve our problem
library(tidyquant) # general R quant package
library(DT) # package to display pretty Data tables

project1_cf <- tibble(Year = 0:5,
cf = c(-800,200,250,300,350,400))

project2_cf <- tibble(Year = 0:5,
cf = c(-500,150,170,178,250,300))

project1_cf %>%
DT::datatable(rownames = FALSE,caption = "Project 1")``````
``````project2_cf  %>%
DT::datatable(rownames = FALSE,caption = "Project 2")``````
``````# To calculate IRR we will use the IRR formula from the jrvFinance library

irr1 <- project1_cf %>%
select(cf) %>%
.[] %>%
irr()

irr2 <- project2_cf %>%
select(cf) %>%
.[] %>%
irr()

irr_tbl <- tibble(Name = c("Project 1", "Project 2"),
IRR = c(irr1,irr2))

irr_tbl %>%
datatable(caption = "Internal rate of returns for the two projects.")``````

From the above table we can see that project 1 has an IRR of 22.1603098 and project 2 has an IRR of 26.7619869. Even though project 1 offers higher cash flows, project 1 has a lower internal rate of returns. If the managers goal is to choose the project that maximizes profitability then he/she should choose project 2.