I want to create a range of values with a /- interval given a mid point. The mid point and intervals are variables and can change.
For example, if my mid point is 0 and my interval is 5, I want my vector to be comprised of
[-5,-4,-3,-2,-1,0,1,2,3,4,5]
If my mid point is 140 and interval is 5, the vector would be
[135,136,137,138,139,140,141,142,143,144,145]
I initially thought this would be easy to do in a single for loop. But I am totally stumped on how to do this in an elegant fashion in R
The only way I can think off is to calculate the negative and positive values separately and then join the elements to form a vector.
CodePudding user response:
You could use this which uses the seq()
function:
mid_int <- function(mid_point, interval) {
seq(mid_point - interval, mid_point interval, by = 1)
}
mid_int(0, 5)
[1] -5 -4 -3 -2 -1 0 1 2 3 4 5
mid_int(140, 5)
[1] 135 136 137 138 139 140 141 142 143 144 145