In other words: 1) get k spatially representative points by performing k-means on a 2D matrix of lng lat 2) for each of these centroids, aggregate some function (e.g. median) of the covariates var over the numClosestPoints closest points spatially to get an idea of the value of var in a neighbourhood

spatialKMeans(
  shp,
  var,
  numCentroids = NULL,
  propToKeep = NULL,
  numClosestPoints = 10,
  aggFct = function(x) {
     median(x, na.rm = T)
 },
  iter.max = 10^3
)

Arguments

var

- string - variable to summarise

numCentroids

number of representative points to take (use EITHER numCentroids or numClosestPoints)

numClosestPoints

- analogous to bandwidth - larger number reduces the variance (use EITHER numCentroids or numClosestPoints)

aggFct

- function to use to summarise - e.g. median or mean

iter.max

max number of iterations for kmeans

Value

shpCentroids with column var