ComenzarEmpieza gratis

Multiple arguments (2)

Let's think about a more complicated example. Do you remember present value from the Introduction to R for Finance course? If not, you can review the video for that here. The idea is that you want to discount money that you will get in the future at a specific interest rate to represent the value of that money in today's dollars. The following general formula was developed to help with this:

present_value <- cash_flow * (1 + i / 100) ^ -year

Wouldn't it be nice to have a function that did this calculation for you? Maybe something of the form:

present_value <- pv(cash_flow, i, year)

This function should work if you pass in numerics like pv(1500, 5, 2) and it should work if you pass in vectors of equal length to calculate an entire present value vector at once!

The percent_to_decimal() function is available for you to use.

Este ejercicio forma parte del curso

Intermediate R for Finance

Ver curso

Instrucciones del ejercicio

  • Fill in the blanks in the function so it does the following:
    • Require the arguments: cash_flow, i, year
    • Create the discount multiplier: (1 + i / 100). Use the percent_to_decimal() function to convert i to a decimal.
    • Perform the present value calculation. Do not store this in a variable. As the last executed line, it will be returned automatically.
  • Calculate the present value of $1200, at an interest rate of 7%, to be received 3 years from now.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# Present value function
pv <- function(___, ___, ___) {
    
    # Discount multiplier
    mult <- 1 + ___(i)
    
    # Present value calculation
    ___ * ___ ^ -___
}

# Calculate a present value
___
Editar y ejecutar código