Have you considered the humble dplyr::summarise()
?
It will merge geometry of your spatial object - either to a single geometry, or multiple ones if you set a grouping variable - and it can do any aggregation, such as calculating total of score in this toy example.
A possible grouping variable is intersection of the polygons with themselves - it seems to work in this example, I hope it will generalise
library(sf)
library(sp)
library(dplyr)
df <- data.frame(x = c(0.5, 1.5, 4.5, 5.5),
y = c(1, 1, 1, 1),
id = c('a', 'b', 'c', 'd'),
score = c(1, 3, 2, 4))
s_df <- SpatialPointsDataFrame(df[, c('x', 'y')], df[, 3:4]) %>%
as('sf') %>%
st_buffer(dist = 1)
plot(s_df)
result <- s_df %>%
group_by(group = paste(st_intersects(s_df, s_df, sparse = T))) %>%
summarise(score = sum(score))
plot(result["score"])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…