I could come up with the number of points within 500m buffer with following function. However, I want to find the number of points in 500m buffer with similar values under variables.
For example under variable Race: there are 3 values, white, hispanic, black and I want to find the number of points within 500 meter which have similar race as the reference point.
library(geosphere)
coordinates <- cbind(data$Y, data$X)
# Calculate distances between each point and all other points
distances <- distm(coordinates, fun =distHaversine)
# Count the number of points within 500 meters
data$proximity <- rowSums(distances <= 500)
You can split your dataset (
split(data, ~Race)) and work with 3 distance matrices instead of one. Though this is bit more convenient withsf&dplyrwhere you can just group points by some attribute and then get the number of points within the distance from each of those locations.Resulting dataset with grouped and ungrouped counts and plot with point buffers. E.g. check "5" : total size of that neighbourhood is 5 while grouped count (number of triangle-points within 5's buffer) is 3.
Created on 2024-03-31 with reprex v2.1.0