# How to compute weighted mean centroid of lat+lon points taking into account spheroid

I have a bunch of points (lat+lon coordinates). Each one has a weight. I would like to get a weighted mean centroid of the points, taking into account the spheroid (as my points are scattered across the United States and I want to roughly adjust for the curvature of the earth).

Say I have these data in R:

``````pts <- data.frame(x=c(-100.5, -98.6, -98), y=c(35, 41, 44), weight=c(20, 15, 100))
``````

How can I take these points and find what I am looking for?

I do not understand `sp`, `sf`, etc. very well. But based on this post (Finding centroid of cluster of points using R in particular the answer by @Ian Turton and @whuber's comment), it appears that we need to do these steps (which I am not really sure how to do):

1. Convert the points to `sp` or something similar
2. Go from lat+lon to a 3d projection (geocentric coordinates). (Possibly using @whuber's answer to this question? Euclidean and Geodesic Buffering in R)
3. Take the weighted average (mean) of the points. This produces the weighted centroid.
4. Convert the weighted centroid back into latitude and longitude.
• what variable does weight represent? population? Commented Aug 19, 2022 at 0:00
• @ElioDiaz, each dot is a university and the weights are number of graduates. Commented Aug 19, 2022 at 4:36
• en.wikipedia.org/wiki/Center_of_population describes 3 method. I use the 3D coordinates and then project (which might be one of the methods mention there) Commented Aug 19, 2022 at 16:52
• Covert to unit sphere vector space - makes things a lot easier. Alternatively, bin to a spherical grid system and apply calculations on aggegated values over cell indices - somewhat biased, but libraries like H3 make this rather convenient. Commented Aug 24, 2022 at 19:58
• Wouldn't you also respect the curvature of the earth when determining a weighted centroid by simply averaging your lat/lon values based on the WGS 84 ellipsoid (EPSG: 4326)? Not really sure why you should reproject to EPSG: 4328 in the first place here. Just to be 100 % consistent with distances from `st_distance()` and `nngeo::st_nn()`? Commented Aug 24, 2022 at 20:19