Suppose I have a vector of cut points, for the purposes of this question, generated as follows:
> seq(0,50,10)
[1] 0 10 20 30 40 50
This is a numeric vector of length 6. I would like to generate a numeric vector of midpoints, to get either of the following (both of which meet my requirements), of length one less of the cuts (in this case, 5).
# midpoints, exactly
5 15 25 35 45
# medians excluding right
4.5 14.5 24.5 34.5 44.5
Not finding a pre-rolled function, I developed a procedure that takes a numeric vector as an argument (the cut points) and returns a vector (the midpoints). It works by taking the median of index 1 and index 2 and appending it to a vector, then the median of index 2 and index 3, and so on until the last index is NULL.
Surely, I can't be the first one to have this need. Is there a package with such a procedure? I don't mind rolling by own, but honestly I'd rather use a package that's been subject to the rigors of public scrutiny.
Thanks
We see that the position vector of the midpoint of the line segment is a kind of average of the position vectors of the end points. We can therefore find the coordinates of the midpoint by finding the average of the x coordinates and y coordinates respectively.
Split the difference?
a <- seq(0,50,10)
a[-length(a)] + diff(a)/2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With