Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate a vector of midpoints or medians from a vector of cut points?

Tags:

r

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

like image 873
ccc31807 Avatar asked Nov 04 '15 23:11

ccc31807


People also ask

What does the midpoint mean in vectors?

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.


1 Answers

Split the difference?

a <- seq(0,50,10)
a[-length(a)] + diff(a)/2
like image 74
Neal Fultz Avatar answered Oct 02 '22 12:10

Neal Fultz