Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Product of vector, in order, result is single number based on last product

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading