R: Determine cell values ​​in matrices

I have a set of 945 geocoded locations here . Each location has an ID and a given collection:

"ID","LON","LAT","POPULATION"
1,86.648064,22.80682,386
2,86.65358,22.81848,655
3,86.670502,22.78508,624
4,86.685028,22.842409,708
5,86.716599,22.866791,987
6,86.734879,22.87911,415
7,86.736687,23.112619,715

      

I am trying to figure out, for each location, the cumulative population of all villages within a 10 km radius.

Calculating distances for each point is trivial:

coords = read.csv("filepath/coords.csv", header=T, stringsAsFactors=F)
coords.matrix = data.matrix(coords[,c(2,3)])

library(sp)
dist = list()
for(i in 1:nrow(coords.matrix)) {
  dist[[i]] = (spDistsN1(coords.matrix, coords.matrix[i,], longlat=T)) #See: http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/sp/html/spDistsN1.html
}
dist = do.call(rbind, dist)

      

which gives me this 945X945 matrix. But how do you link these cells to IDs and corresponding groups?

+3


source to share


1 answer


Assuming the matrix dist

has distances in KM, I think you can do it like this:

coords$POPIN10KM <- sapply(1:nrow(dist),function(i)sum(coords$POPULATION[dist[i,]<10]))

      



Which adds a column to your dataframe indicating a population within 10 km

+3


source







All Articles