6.1 Simple Calculations

Suppose the function that need multiple calculations of \(1 + i*t\) is needed. Suppose \(i = 0.05\) and \(t = 2\).

Then you could create the following code, where accum holds the result of the calculation of \(1 + i \cdot t\) for the values of \(i\) and \(t\).

i <- 0.05
t <- 2
accum <- 1 + i*t
accum

Calculate the value of accum yourself to verify the result.

Now suppose you insert the code for accum before \(i\) and \(t\) are defined. What would happen? Here I create a new value of accum1 that is a function of \(d\) and \(n\) (that have not been set ahead of time).

accum1 <- 1 + d*n

When you run this code, you will receive an error, as \(d\) and \(n\) have not yet been defined. (Here the error initially describes that \(d\) is missing, however once you defined \(d\), then another error would result as you had not yet defined \(n\).) Thus, the order of input of the code is important.

You can modify the function:

accum2 <- (1 + i)^t
accum2

Here, the function exponentiates the quantity \((1 + i)\) to the \(t\)th power.