I'm a beginner in R.
I am working in R with a dataset of bird nest locations and am interested in the distribution of said nests. Specifically, I'd like to find the closest neighbor to each nest as well as the distance. There is a similar discord titled "R - Find "n" closest points to each point in SpatialPointsDataFrame" that has been helpful but I need to take it further. I have multiple years of data and need to find the closest neighbor within a given year. As well, I'd like to only consider neighbors outside of the current nest's territory. So for each nest, I'd like to find the closest nest within the same hatch year and exclude nests in the same territory. This filtering per se is what I am struggling with.
My reproducible input:
df <- structure(list(NEST_NUMBER = c("LOSH-2019-01", "LOSH-2019-02", "LOSH-2019-03", "LOSH-2019-04", "LOSH-2019-05", "LOSH-2019-06", "LOSH-2019-07", "LOSH-2019-08", "LOSH-2019-09", "LOSH-2019-10", "LOSH-2019-11", "LOSH-2019-12", "LOSH-2019-13", "LOSH-2019-14", "LOSH-2019-15", "LOSH-2019-16", "LOSH-2019-17", "LOSH-2019-18", "LOSH-2019-19", "LOSH-2019-21", "LOSH-2019-22", "LOSH-2019-23", "LOSH-2019-24", "LOSH-2019-25", "LOSH-2019-26", "LOSH-2019-27", "LOSH-2019-29", "LOSH-2019-30", "LOSH-2019-31", "LOSH-2019-32", "LOSH-2019-33", "LOSH-2019-34", "LOSH-2019-35", "LOSH-2019-36", "LOSH-2019-37", "LOSH-2019-38", "LOSH-2019-39", "LOSH-2019-40", "LOSH-2019-41", "LOSH-2019-42", "LOSH-2019-43", "LOSH-2019-44", "LOSH-2020-01", "LOSH-2020-02", "LOSH-2020-03", "LOSH-2020-04", "LOSH-2020-07", "LOSH-2020-08", "LOSH-2020-09", "LOSH-2020-10" ), TERRITORY = c("RIVERF", "DISCGO", "SCHOLA", "COXFER", "LOWESP", "SEACOA", "HGTCCC", "WPDEDI", "CMWSSS", "IMSTOP", "ROOMST", "OAKFOR", "CEMETE", "COSTCO", "DILLAR", "RIVERF", "CYPRES", "BUSBEE", "TEKNOW", "MATTRE", "MARINA", "COXFER", "SEACOA", "WPDEDI", "CYPRES", "DISCGO", "TEKNOW", "SCHOLA", "HGTCCC", "COOKOU", "LOWESP", "COSTCO", "METGLA", "RIVERF", "ROOMST", "IMSTOP", "CMWSSS", "WPDEDI", "SCHOLA", "ABUELO", "SEACOA", "COXFER", "LOWESP", "TANGER", "501WAL", "DISCGO", "OAKFOR", "HGTCCC", "SCHOLA", "CYPRES"), LAT = c("33.8313", "33.7975", "33.7963", "33.8071", "33.7924", "33.8075", "33.7936", "33.800678", "33.799952", "33.7998", "33.415943", "33.421264", "33.7916", "33.702579", "33.415945", "33.8316", "33.8319", "33.8288", "33.7982", "33.759131", "33.748762", "33.8072", "33.8077", "33.8005", "33.8337", "33.79785", "33.7968", "33.797108", "33.7935", "33.7876", "33.7925", "33.70257", "33.79346", "33.831432", "33.704105", "33.8001", "33.7994", "33.8008", "33.796989", "33.7044", "33.8074", "33.8071", "33.7922268", "33.751229", "33.77855", "33.797473", "33.703563", "33.793694", "33.794193", "33.834228"), LONG = c("-79.0466", "-79.0038", "-79.0057", "-79.0085", "-78.9978", "-79.0132", "-79.0015", "-79.000599", "-78.998438", "-78.9945", "-78.553058", "-78.543716", "-79.0197", "-78.919028", "-78.553058", "-79.0463", "-79.046", "-79.0571", "-78.9908", "-78.969619", "-78.842995", "-79.0088", "-79.0126", "-79.0009", "-79.0463", "-79.003976", "-78.9913", "-79.005799", "-79.0016", "-78.9949", "-78.9979", "-78.919004", "-78.99601", "-79.046285", "-78.925926", "-78.9943", "-78.9978", "-79.0005", "-79.005587", "-78.9243", "-79.0126", "-79.0087", "-78.997713", "-78.960369", "-78.987055", "-79.003753", "-78.90968", "-79.001613", "-79.006844", "-79.04458"), HATCH_DATE = structure(c(17981, 17989, 17992, 18003, 17987, 17990, 17998, 18003, 18010, 18015, 17992, 18007, 17989, 17987, 17995, 18010, 18003, 18014, 18012, 18037, 17994, 18045, 18047, 18036, 18037, 18037, 18058, 18051, 18057, 18057, 18051, 18042, 18058, 18066, 18056, 18066, 18075, 18072, 18065, 18071, 18084, 18087, 18343, 18347, 18362, 18360, 18358, 18369, 18354, 18360), class = "Date"), HD_YEAR = c("2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020")), row.names = c(NA, 50L), class = "data.frame") Desired output: Dataframe containing a row for each nest and columns of first nearest nest, second nearest nest, third nearest nest as well as the distances between those and the original nest.
I attempted using a distance matrix with my data, but that compares all nest together and doesn't take into account year or territory.
df |> head(10) |> dput()on your input and edit your question with the object provided? This would make your question reproducible for others.