I'm working with the sf package in R to process a set of point geometries. My objective is to find neighbors for each point within a certain distance. However, I'm encountering an error when attempting to subset geometries.
Here's a simplified version of my code:
library(sf)
# Load the shapefile
points_sf <- st_read("path_to_my_shapefile.shp")
# Define a function to get points within a buffered geometry
get_points_within <- function(geometry, dataset, buffer_distance) {
buffered_geometry <- st_buffer(geometry, dist = buffer_distance)
return(dataset[st_within(st_sfc(geometry, crs = st_crs(dataset)), buffered_geometry), ])
}
# Extract geometries from the sf object
geometries <- st_geometry(points_sf)
# Test the function with an individual geometry
single_geometry <- geometries[1]
neighbors <- get_points_within(single_geometry, points_sf, 6)
When running the above code, I get the following error:
Error in `[.default`(geom, i) : invalid subscript type 'list'
From the traceback, it seems that the issue is related to the st_within() function call within the get_points_within function.
Can someone help me understand why this error is occurring and how to resolve it?