# How to Calculate the Net Present Value (NPV) in R [Examples]

In this article, we discuss how to calculate the Net Present Value (NPV) in R.

The Net Present Values is a valuation method for investment projects in terms of cash inflow and cash outflow. It accounts for the time value of money by discounting all the cash flows back to today, i.e., their Present Value (PV).

In short, the Net Present Value is the difference between the present value of the cash inflows and the present value of the cash outflows. Therefore, if the NPV is greater than zero, then the investment is favorable.

But, how do you calculate the NPV in R?

In R, the easiest way to calculate the Net Present Value is by using the NPV function. This function computes the NPV given the initial cash flow, future cash flows, and the interest rate. Optionally, it shows a diagram of the cash flows.

In this article, we demonstrate how to use the NPV() function, as well as how to calculate the Net Present Value with basic R code. Additionally, we show how to calculate the NPV for a project of cash flows with different discount rates.

## How to Calculate the Net Present Value (NPV) in R

Below we discuss two ways to find the NPV of a project in R.

Our fictitious project has an upfront investment of \$100. In the following years, we expect cash inflows of \$5, \$20, \$25, \$30, and \$50. Also, we assume a constant discount rate of 5%.

``````library("tidyverse")
cashflows <- tibble(time = c(0:5),
cashflow = c(-100, 5, 20, 25, 30, 50),
discount_rate = rep(0.05, 6))
cashflows %>% print()``````

### Calculate the Net Present Value with the NPV Function

The easiest way to calculate the Net Present Value in R is with the NPV() function.

This function computes the NPV of an investment given 4 mandatory arguments:

1. cf0: The cash flow at moment zero, i.e., the initial investment.
2. cf: A vector of future cash flows.
3. times: The moments when you expect the future cash flows.
4. i: The discount rate.

The NPV() function is part of the FinancialMath package which contains many functions for financial analysis.

If we use the NPV() function to calculate the Net Present Value of our fictitious project, we need the following R code.

``````library("FinancialMath")
NPV(cf0=100, cf=c(5, 20, 25, 30, 50), times=c(1:5), i=.05)``````

Since the NPV is greater than zero, this is a good investment.

Optionally, the NPV() function can plot a diagram of the cash flows. By default, the plot argument is set to FALSE. However, if you use plot=TRUE, the NPV() function adds a plot of all the cash flows which can be helpful for presentations.

``NPV(cf0=100, cf=c(5, 20, 25, 30, 50), times=c(1:5), i=.05, TRUE)``

### Calculate the Net Present Value with Basic R Code

Instead of using the NPV() function, you can also compute the NPV with Basic R Code.

Although this method requires more coding, it might help people who are not familiar with the NPV to understand your code.

Below we show how to calculate the Net Present Value step by step.

First, we calculate the present value of each cash flow, i.e., we discount for the time value of money.

``````library("tidyverse")

# Calculate the Present Value of Each Cash Flow
cashflows <- cashflows %>%
mutate(cashflows, present_value = cashflow / (1+discount_rate)^time)``````

Next, we sum the present value of each cash flow. Finally, we print the Net Present Value to the R console.

``````# Sum all the Present Values
net_present_value <- cashflows %>%
select(present_value) %>%
sum()

# Print the Net Present Value (NPV)
net_present_value``````

## How to Calculate the NPV with Different Discount Rates in R

So far, we have assumed that the discount rate is constant for each period. However, this assumption might not be valid. So, how do you calculate the Net Present Value with different discount rates?

Although the NPV() function is convenient, it can’t calculate the Net Present Value when we deal with multiple discount rates. Therefore, you have to write your own R code.

In the example below, we expect the same initial investment and future cash flows. However, the discount rate is not stable. Instead, we will use the following scenario.

The first step to compute the NPV of a project with different discount rates is to find the correct discount factor for each period. Therefore, we will use a for-loop.

``````cashflows <- cashflows %>%
mutate(discount_factor = 1)

for (row in 2:dim(cashflows)[1]) {
cashflows[row,] <- mutate(cashflows[1:row,], discount_factor= lag(discount_factor,1) / (1+discount_rate))[row,]
}

cashflows``````

The for-loop is necessary because the discount factor of the current period depends on the discount factor of the previous period.

For example, the discount factor of the fourth period is:

These are the discount factors of our project.

Next, we calculate the present value of each cash flow using the discount factor instead of the discount rate.

``````# Calculate the Present Value of Each Cash Flow
cashflows <- cashflows %>%
mutate(cashflows, present_value = cashflow * discount_factor)``````

Finally, we compute the sum of all the present values to find the Net Present Value

``````# Sum all the Present Values
net_present_value <- cashflows %>%
select(present_value) %>%
sum()

# Print the Net Present Value (NPV)
net_present_value``````

Because the NPV is lower than zero, this is not a good investment.