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 create a sequence of numbers in R from min to max with zero as the midpoint?

If I had the following variable.

set.seed(20)
variable <- sample(-74:124.2, size = 38)

How can I create a sequence of 5 numbers using the min and max values of variable and zero as the midpoint (3rd value)?

seq() takes the start and end values, but is there a way to do this by specifying a middle value?

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

seq(min(variable), max(variable), length.out = 5)

>Solution :

You can replace the median of the generated sequence with a specified value:

seq(min(variable), max(variable), length.out = 5) -> x        
x[which(x == median(x))] <- 0

#-73.00 -25.75   0.00  68.75 116.00

This would work if the length(x) is odd.

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