# [R-sig-Geo] Population-weighted Centroids of Polygons in R

Jose M. Pavia Jose.M.Pavia at uv.es
Tue Feb 23 21:47:53 CET 2016

```If I understand your problem. You can compute a global centroid for the large polygon directly as a weighted average.

If Pi is the population of block i
and (xi, yi) are the coordinates of the centroid of block i.

the weighted average centroid of the polygon will be:

x_w = sum(xi*Pi)/sum(Pi)
y_w = sum(yi*Pi)/sum(Pi)

where the sum is over all the blocks in the corresponding polygon.

best regards
jm

> Hi Loïc,
>
> Thank you so much for your reply. I went through the approach that you have already proposed. I believe it's a great idea.
> I'd like to emphasize that the coordinates of the centroid of each block is available. The idea is how to relate these centroids to the corresponding population of the blocks (which are all inside a larger polygon, of course) and to find a global centroid for the large polygon which could reflect the effect of the population density in the blocks.
> I also wonder if I can find way to compute the "weighted average" using a kind of distance function.
>
> In this regard, any suggestions would be welcome.
>
> Sincerely,Sohrab
>
>     On Sunday, February 21, 2016 11:45 PM, Loïc Dutrieux <loic.dutrieux at wur.nl> wrote:
>
>
>  Hi Sohrab,
>
> I don't think there is a method for weighted centroids specifically.
> What if you get the centroid coordinates of each "block" using
> gCentroid() and calculate a weighted average of the coordinates of every
> centroid?
> See an example below assuming your blocks are a
> SpatialPolygonsDataFrame. It's quite a simple approach; not sure how
> valid it would be with more complex features though (eg: with holes, etc)..
>
> Cheers,
> Loïc
>
> library(rgeos)
> library(sp)
>
> # Generate SpatialPolygonsDataFrame object (blocks with population data)
> cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
> cds2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0))
> sp <- spPolygons(cds1, cds2)
> spdf <- SpatialPolygonsDataFrame(sp, data.frame(pop = c(25,32)))
>
> # Get centroid of every polygon
> centroids <- gCentroid(spdf, byid = TRUE)
>
> # Get a dataframe with coordinates of centroids and weights
> df <- cbind(data.frame(centroids), spdf at data)
>
> # SpatialPoints corresponding to Weighted centroid
> SpatialPoints(data.frame(x = weighted.mean(df\$x, df\$pop), y =
> weighted.mean(df\$y, df\$pop)))
>
> On 02/21/2016 09:10 PM, Sohrab Abbasnejad via R-sig-Geo wrote:
> > Dear all,
> > I have a set of spatial polygons and several blocks of population (with fixed area but a different number of people in each). I have used the "over" method from "sp" package to identify the blocks which are located inside these polygons.
> > Now, utilizing R, I wish to assign to each of these polygons a centroid in terms of the population using some kind of distance (Euclidean/shortest path, etc.). In fact, I am looking for the exact coordinates of a population-weighted centroid for each of my polygons.
> > Any ideas would be welcome. I truly appreciate your time.
> > Sincerely,Sohrab
> >     [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo at r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

--
Jose M. Pavia
Quantitative Methods' Professor
Universitat de Valencia
Av Tarongers, s/n
46022 Valencia, Spain
Phone: +34 963 828 404
Fax: +34 963 828 415
e-mail: pavia at uv.es
http://www.epo-uv.es/

The combination of some data and an aching desire for
be extracted from a given body of data.~ John Tukey

CONFIDENTIALITY CLAUSE
This message has been sent from a Universitat de Valencia
email account for the institution’s own purposes. Its content
and any files transmitted with it are intended solely for
the use of the individual or entity to whom they are addressed
and are considered confidential, except where justified by their
specific nature. If you are not the named addressee, you should
not disseminate, distribute or copy this email without consent.