'r spatial join-multiple criteria

I have two sf datasets and I want to find the 10 closest neighbors, based not only on distance but also on mathcing another column. For example:

a = st_sf(a = 1:3, DD=c("d1","d2","d3"),
 geom = st_sfc(st_point(c(1,1)), st_point(c(2,2)), st_point(c(3,3))))
b = st_sf(a = 11:14,DD=c("d1","d2","d2"),
 geom = st_sfc(st_point(c(10,10)), st_point(c(2,2)), st_point(c(2,2)), st_point(c(3,3))))

I would like to find the neighbors of "a" in "b" having the same value for "DD" col. Currently I am using this approach based on sf and nngeo:

st_join(a, b, join = st_nn, k = 1, progress = FALSE)

But this joins points based on geometry only and I do not know how to get DD into account as well.

Thank you!

I have posted the question also on gis.spatexchange



Sources

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

Source: Stack Overflow

Solution Source