[R-sig-Geo] center of mass for variable values in a grid

Waichler, Scott R Scott.Waichler at pnl.gov
Mon Sep 13 22:00:59 CEST 2010


> >>  If your data is in a data frame of coordinates x, y, (and maybe z),
> >> and variable W for all cells, and all your cells are the same size,
> >> then its just the weighted average of the coordinate (assuming your
> >> dataframe is the complete grid). So:
> >>
> >>  xm = sum(d$x*d$W)/sum(d$W)
> >>  ym = sum(d$y*d$W)/sum(d$W)
> >
> > You're right, it's simple for a grid where dx and dy are constant.
>  Thanks.  But if they vary?
> 
>  Treat the masses as points centred at each grid cell and then find
> the weighted mean of X and Y as before.

Sorry, I was wrong in thinking that a regular grid is needed.  The simple weighting scheme above will work for any set of points, as long as the weight represented at each point is centered there.

Scott Waichler



More information about the R-sig-Geo mailing list