'R : for grouped observations, checking if their buffers intersect (finding duplicates)

I have a dataframe with lots of observations, and I want to remove duplicates. I have a sf variable geometry with coordinates, that I used to create buffers like this :

data$geometry = st_transform(data$geometry, 3857)
data$polygons = st_buffer(data$geometry, 500)

To check for duplicates, first, I run this code (from the package janitor) :

data <- get_dupes(data, date, name)

Which isolates the observations with duplicates on date and name.

Then, I want to group my observations by this two variables (date and name), and then ask if within each group the polygons intersect (variable polygons). If true, I want a new variable duplicate to indicate 1. If false (no intersection), I want duplicate = 0.

I already tried to run this code :

data$intersect <- st_intersects(data$polygons, sparse = TRUE)

But it only returns for each observation a list of the observations' ID whose buffer intersect with, and I don't know how to use it to have the result I want above (with the creation of a new variable duplicate).

I don't know if what I want is understandable. Thank you for your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source