A vector:
y <- c(1.1, 1.05, 1.01)
initial_amount <- 100
I would like to multiply initial_amount by the vector in the following way:
initial_amount * 1.1 * 1.05 * 1.01
[1] 116.655
Suppose I have a vector y and initial_amount numeric variable. How can I tell r to compute a final amount in this way? Using the example the result would be 116.655
>Solution :
We can create a vector by concatenating the ‘initial_amount’ with ‘y’ and use prod
prod(c(initial_amount, y))
[1] 116.655
Or may use Reduce with *
Reduce(`*`, c(initial_amount, y))
[1] 116.655