Like the title said, I made a plot in R containing datasets with different X and Y coordinates. On the plot I can see some overlapping dots, but I don't know how to find these overlapping dots in the plot, as the X and Y coordinates are not the same.
I have tried to ask for help using ChatGPT like this:
library(sp)
# Example datasets (replace these with your actual datasets)
dataset1 <- data.frame(x = c(1, 3, 5, 7), y = c(2, 4, 6, 8))
dataset2 <- data.frame(x = c(3, 5, 9), y = c(4, 6, 10))
# Convert datasets to spatial points data frames
coordinates(dataset1) <- ~x+y
coordinates(dataset2) <- ~x+y
# Define the distance threshold for considering points as overlapping
distance_threshold <- 1.0 # Adjust as needed
# Find overlapping coordinates
overlapping_coordinates <- sp::over(dataset1, dataset2, fn = NULL, sparse = FALSE)
# Filter overlapping coordinates based on distance threshold
overlapping_coordinates <- overlapping_coordinates[sapply(1:nrow(overlapping_coordinates), function(i) {
point1 <- coordinates(overlapping_coordinates)[i, ]
point2 <- coordinates(overlapping_coordinates)[i + nrow(dataset1), ] # Offset to point in dataset2
d <- sqrt((point1[1] - point2[1])^2 + (point1[2] - point2[2])^2)
d <= distance_threshold
}), ]
# Extract non-overlapping coordinates
non_overlapping_dataset1 <- dataset1[!rownames(dataset1) %in% overlapping_coordinates[, 1], ]
non_overlapping_dataset2 <- dataset2[!rownames(dataset2) %in% overlapping_coordinates[, 2], ]
# Display the results
print("Overlapping Coordinates:")
print(overlapping_coordinates)
print("\nNon-overlapping Coordinates from Dataset 1:")
print(non_overlapping_dataset1)
print("\nNon-overlapping Coordinates from Dataset 2:")
print(non_overlapping_dataset2)
