I am trying to calculate the average distance to all habitat patches within a landscape from a site point (sf object). To do this, I have polygonized my habitat raster (dissolving to get patches) and am hoping to use st_distance()
to calculate the distance from my points to each habitat patch (with 4 possible habitat types).
When I polygonize my raster and convert to an sf object, the resulting data frame looks like this:
habitats.sf <- rasterToPolygons(habitat.raster, dissolve = TRUE) %>%
st_as_sf()
view(habitats.sf)
Each layer has a number of geometries corresponding to each patch within the layer.
When I calculate the distance between my points and these habitat polygons, the result looks like this:
st_distance(sites.sf, habitats.sf, by_element = FALSE)
with each matrix row corresponding to a site, and each column corresponding to a habitat type.
Is the st_distance()
calculation calculating the distance to the closest polygon of each class, or the mean distance of all of the polygons of a given class? If the former, how can I pivot my habitats.sf data frame such that each row only contains a single polygon geometry rather than a list so that I can calculate the distance to ALL patch geometries within each habitat type?