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

How to generate a v shaped sequence in R without using cbind

I’m working through r-exercises. One requires generating the following vector:

[0.05, 0.5, 5.0, 0.5, 0.05]

The catch is cbind or loops cannot be used.

Here is what I tried:

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

rep(0.05 * 10^(seq(0,2)), times=2)

But of course that just returns:

[1] 0.05 0.50 5.00 0.05 0.50 5.00

Any ideas?

>Solution :

You can apply abs to a sequence centered at 0 to symmetrize it:

5*10^-abs(-2:2)
## [1] 0.05 0.50 5.00 0.50 0.05
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