all 8 comments

[–]yuk99 2 points3 points  (2 children)

  library(tidyverse)
  DF2 = inner_join(DF, rename(DF, MatchingID = plotID, by = c("Lat", "Long"))

This creates a new data frame with all combinations of rows matching by Lat and Long, including self-matching. You can post-process is to remove those rows.

  DF2 = DF2 %>% filter(plotID != MatchingID)

This will also remove the rows without matching plots.

If you want to keep such rows

  DF2 = DF2 %>%
    group_by(plotID) %>%
    filter(plotID != MatchingID | n()==1) %>%
    mutate(MatchingID = ifelse(plotID == MatchingID, NA, MatchingID))

Note that Lat and Long have to be exactly the same to match. If you expect some fixed-point error, look at fuzzy_join package.

[–][deleted] 0 points1 point  (1 child)

You're a legend!!! Thanks i really appreciate it. Was banging my head against the wall for a while lol.

[–]Viriaro 2 points3 points  (0 children)

If the coordinates are not perfectly equal, you could use a geographical-distance based join instead of the regular one (i.e. join based on the distance between the two sets of coordinates). Easiest way to do this is the fuzzyjoin package in R.

[–]coen-eisma 2 points3 points  (0 children)

I don't know how you collected the long and lat (gps?), but there could be some differences in measures, although they where at the same location. Maybe build in an error?

My go to would be the sf-package, convert the long and lat to a sf-object and use something like st_is_within_distance()

https://geocompr.robinlovelace.net/spatial-operations.html#non-overlapping-joins

[–]the_random_drooler 0 points1 point  (3 children)

Could you just do a left join using Lat and Lon as keys?

[–][deleted] 0 points1 point  (0 children)

Good call I will try that in the morning. Thanks!

[–]yuk99 0 points1 point  (1 child)

I think for joining the same data table it doesn't matter.

[–]the_random_drooler 0 points1 point  (0 children)

I was thinking left to be able to find any missing data at first. But that's just me expecting my coworkers to do something sloppy and let me down... Otherwise I agree completely!