I'm interested in extracting count data from a list of linear features based on their spatial overlap with buffered point data. I have several thousand points, each of which I buffer with 100m radius. I then use st_intersects to identify the linear features that intersect each buffer. Next, I would like to use the feature ids to extract count data from a separate dataframe for each of these features. I want to sum them together so that I have the total counts for all features that intersect each buffered point.
St_intersects gives me a list, with a single row for every buffered point, and that list contains the index (or id) of the linear features that intersect the buffer. I'm not sure how to use this list to reference a separate dataframe and sum all count data associated with those linear features and store them in a column with the original point data.
points # sf object, point data
linear_features # spatial line data
counts # dataframe with linear feature ids and count data based on use
intersect_100 <- points %>% st_buffer(.,100) %>%
st_intersects(., linear_features) %>% # this gives index of overlapping linear features
lapply(., function(n){linear_features$id[n]}) # this gives list of ids that intersect with each buffer region
I would now like to write a loop so that the 'points' dataframe has a new column called "total_counts" based on the counts of all linear features within 100m. These count data are stored in a separate dataframe called 'counts'. The 'linear_features' and 'counts' both have the same set of ids...
Thanks so much for any help!