# find the centroid of a cluster of points

While searching the web, solutions for finding centroids of polygons come up rather often. What I'm interested in is finding a centroid of a cluster of points. A weighted mean of sorts. I would appreciate it if someone could provide some pointers, pseudo code (or even better, an R package that has already solved this) or links of how this issue can be tackled.

EDIT

Convergence has been afoot (again). iant has suggested a method to average coordinates and use that for the centroid. This is exactly what crossed my mind when I saw the right picture on this web page.

Here is some simple R code to draw the following figure that demonstrates this (× is the centroid):

``````xcor <- rchisq(10, 3, 2)
ycor <- runif(10, min = 1, max = 100)
mx <- mean(xcor)
my <- mean(ycor)

plot(xcor, ycor, pch = 1)
points(mx, my, pch = 3)
`````` EDIT 2

`cluster::pam()\$medoids` returns a medoid of a set of cluster. This is an example shamelessly stolen from @Joris Meys:

``````library(cluster)
df <- data.frame(X = rnorm(100, 0), Y = rpois(100, 2))
plot(df\$X, df\$Y)
points(pam(df, 1)\$medoids, pch = 16, col = "red")
``````
• Is there a reason the mean center or center of minimum distance of the points won't suffice? – Andy W Feb 10 '11 at 15:58
• @Roman: The graphic is incorrect: you need to use the mean, not the median. For 2D spatial point clouds there are analogs of a median center, but this is not one of them (because it is coordinate-dependent): see stats.stackexchange.com/q/1927/919 for a discussion. – whuber Feb 10 '11 at 17:34
• I would also suggest checking out chapter 4 of the crimestat workbook, icpsr.umich.edu/CrimeStat/files/CrimeStatChapter.4.pdf. It is a pretty gentle intro, describes and graphically displays why the median for higher dimensions does not have a unique solution, and describes other measures of central tendency and variance of spatial point patterns. – Andy W Feb 10 '11 at 17:53
• This is getting more and more interesting. Thank you for your answers. I'm looking into the matter. – Roman Luštrik Feb 10 '11 at 18:50
• "suggested a method to average coordinates and use that for the centroid." This is, in fact, the definition of the centroid, not simply something which makes a good approximation. – Colin K Feb 10 '11 at 21:06