在 R 中:找到 group_by 内最接近的值,不包括自我比较

In R: find the closest value within group_by excluding self comparisons

我想在与同一列中的每一行具有绝对最小差异的列中找到值。

我试过 and return index from a vector of the value closest to a given element

的解决方案

我的代码:

library(DescTools)
data %>% 
  select(river, dist, id) %>%
  group_by(river) %>% 
  mutate(NNdist = Closest(dist, dist))

对于id = TYWI03我期望NNdist = 1690id = TAFF04我期望NNdist = 1607,但是返回的值是参考值即返回a来自 Closest(x, a)

data

 river  dist id     NNdist
  <chr> <dbl> <chr>   <dbl>
1 Tywi     34 TYWI03     34
2 Tywi   1690 TYWI02   1690
3 Tywi   1747 TYWI01   1747
4 Taff   1607 TAFF05   1607
5 Taff   4341 TAFF04   4341
6 Taff  12357 TAFF03  12357
7 Taff  16111 TAFF02  16111
8 Taff  18124 TAFF01  18124

用我多年前问的问题回答了它

temp1 <- data%>%
  group_by(river) %>%
  mutate(n_ds = match(dist,sort(dist))-1) %>%
  mutate(closest_uid=apply(sapply(dist, function(i)abs(i-dist)), 2, function(n) id[which(n==sort(n)[2])])) %>%
  data.frame()

tempdist <- temp1 %>% select(dist, id) %>% rename(rivDist = dist)

temp2 <- temp1 %>% left_join(tempdist, by = c('closest_uid' = 'id')) %>%
  mutate(mindist = abs(dist - rivDist)