[R-sig-Geo] calculate geometric mean of mass of points

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Nov 19 22:05:59 CET 2008


2008/11/19 Dylan Beaudette <debeaudette at ucdavis.edu>:
> On Wednesday 19 November 2008, Ashton Shortridge wrote:
>> On Wednesday 19 November 2008 02:33:46 pm Alessandro wrote:
>> > I have a points shape and I wish to calculate the geometric centre of
>> > this points shape and create a new points with R. But It's over my
>> > knowledge. Is there suggestions?
>>
>> This seems straightforward, even if it is not implemented in a package.
>> Wikipedia has some basic info on the geometric mean:
>> http://en.wikipedia.org/wiki/Geometric_mean
>>
>> All you seem to have to do is read the shapefile in, grab the x's and y's
>> in two vector (called, say, x and y) and then:
>> totalX <- 1
>> for(i in x) {totalX <- totalX * i}
>> totalY <- 1
>> for(i in y) {totalY <- totalY * i}
>>
>> geomnX <- totalX^(1/length(x))
>> geomnY <- totalY^(1/length(y))
>>
>> You could then make an sp object out of this pair and export it as a
>> shapefile, if you wished.
>>
>> There is probably something slick you could do with lapply or something to
>> avoid the for loops in the code above, but I'm doing this off the top of my
>> head.
>>
>> Also note that if any of your coordinate values are zero then the mean will
>> be zero and may not represent a centered value very well. Also note that
>> negative coordinate values are problematic. This solution assumes that all
>> your coordinates are positive.
>>

 I have a feeling the original poster really wants to compute the
_centroid_, not the geometric mean of the coordinates. The phrase
"geometric centre of this points shape" sounds more like the centroid
than the geometric mean of the coordinate values.

 The centroid is the 'centre of mass' of a polygon - the point where a
cardboard cutout of the polygon would balance.

 The maptools package has a function for computing the centroids of
polygons - install that and try help.search("centroid").

 Otherwise it's quite easy to compute yourself, google for algorithms.
Note it is NOT just the mean of the coordinates!

Barry




More information about the R-sig-Geo mailing list