I have 2 datasets one for hospitals and another for procedures. Each dataset has latitude and longitude coordinates. Procedures are either given in or out of the hospital, though the coordinates are not necessarily precise if given in the hospitals. Im trying to form a radius of a certain size around each of the hospitals and determine how many procedure points fall within that radius on average. So if for example I have 100 hospitals and 3000 procedures, I want to form a radius around all hospitals and see on average how many hospitals fall into that specified radius. My initial code is below, but I know this can be done faster. coded in R. Thanks!
for(i in 1:NROW(hospitals)){
hospital <- hospitals[i,]
radius <- .016
# find all the procedures that lie in the .016 sized radius from this hospital
hospital$latitude_low <- hospital$lat - radius
hospital$longitude_low <- hospital$long - radius
hospital$latitude_high <- hospital$lat + radius
hospital$longitude_high <- hospital$long + radius
in_rad <- procedures[(procedures$long >= hospital$longitude_low & procedures$long <=
hospital$longitude_high & procedures$lat <= hospital$latitude_high & procedures$lat >=
hospital$latitude_low),]
num <- NROW(in_rad)
hospitals[i,]$number_of_procedures <- num
}