Oki, consider this code
What it does is:
- produces a data frame of 3 semi random North Carolina locations (just because I like NC)
- creates a buffer of 2 arc degrees around Greensboro; two of the locations will be in and one out
- creates a leaflet map to visually check the expected count (two out of three red points are in)
- does the actual counting by spatially joining the
buffer and cities objects, and counting the number of rows
library(sf)
library(dplyr)
library(leaflet)
# three semi random cities in NC
cities <- data.frame(name = c("Raleigh", "Greensboro", "Wilmington"),
x = c(-78.633333, -79.819444, -77.912222),
y = c(35.766667, 36.08, 34.223333)) %>%
sf::st_as_sf(coords = c("x", "y"), crs = 4326)
# buffer of 2 arc degrees around Greensboro
buffer <- sf::st_buffer(cities[2, ], 2)
# a leaflet call, looking sorta kinda like yours
leaflet() %>%
addProviderTiles("Stamen.Toner") %>%
addPolygons(data = buffer) %>%
addCircleMarkers(data = cities,
color = "red",
fillOpacity = 2/3,
stroke = F)
# this is the action! :)
count <- sf::st_join(cities, buffer, left = F) %>%
nrow()