I am interested in calculating the proportion of GPS points that fall within protected areas. I can do this with my full data set with the code I show below.
However, I would like to group this data by the id column and get a measure of overlap for A and B separately.
Thanks
# load packages
library(wdpar)
library(dplyr)
library(sf)
# download protected area data
# (excluding areas represented as point localities)
sa_raw_pa_data <- wdpa_fetch(c("South Africa"))
sa_raw_pa_data
# reproject data
sa_pa_data <- st_transform(sa_raw_pa_data, 4326)
# plot it
# plot(sa_pa_data)
# create some random data
set.seed(123)
d = data.frame(long=runif(100,16,38), lat=runif(100,-47,-22))
d$id <- c(rep("A", 50), rep("B", 50))
# measure the overlap
pts <- st_as_sf (d, coords = c ("long", "lat"), crs = 4326)
res <- st_intersects (pts, sa_pa_data)
length (unlist (res)) / nrow (pts) # fraction of intersecting points