Count Points within Polygons
count_points_in_polygons.Rd
Counts the number of points per species within each polygon. If the points dataset has a `species` column, a separate column is created for each species with counts. Spaces in species names are replaced with underscores for column naming.
Value
An `sf` object containing the original polygons and additional columns for each species count.
Examples
if (FALSE) { # \dontrun{
library(sf)
# Example points
points_sf <- st_as_sf(data.frame(
id = 1:6,
species = c("Panthera onca", "Panthera onca", "Felis catus",
"Felis catus", "Felis catus", "Panthera leo"),
x = c(1, 2, 3, 4, 5, 6),
y = c(1, 2, 3, 4, 5, 6)
), coords = c("x", "y"), crs = 4326)
# Example polygons
polygons_sf <- st_as_sf(data.frame(
id = 1:2,
geometry = st_sfc(
st_polygon(list(rbind(c(0,0), c(3,0), c(3,3), c(0,3), c(0,0)))),
st_polygon(list(rbind(c(3,3), c(6,3), c(6,6), c(3,6), c(3,3))))
)
), crs = 4326)
# Count points per species within polygons
result <- count_points_in_polygons(points_sf, polygons_sf)
print(result)
} # }