在特性上找到最接近的三个

问题描述

library("tidyverse")
demo1 <- as.data.frame(tribble(
  ~cut,~freq,~distance,"Fair",1610,4,"Good",4906,100,"Very Good",12082,45,"Premium",13791,50,"Ideal",21551,34,"Very good",14938,60
))

demo2 <- as.data.frame(tribble(
  ~cut,403,14,3920,25,5938,5,4593,40,2.4,))

对于demo1数据中的每个观察(行),我想找到与demo2数据中的行相关的距离最近的三个。结果需要合并到同一个.csv文件中。

我实现的代码显示错误。

Result<-data.frame()
for (k in 1:nrow(demo1)) {
  Result <- demo2 %>% 
    filter(abs(demo2$distance-demo1$distance[k])????)
  print(Result)
  }

有人可以帮我吗,谢谢!

解决方法

也许您可以尝试下面的代码

out <- bind_cols(
  demo2,tibble(nearest = lapply(
    split(
      abs(do.call("-",expand.grid(demo1$distance,demo2$distance))),ceiling(seq(nrow(demo1) * nrow(demo2)) / nrow(demo1))
    ),function(x) demo1[head(order(x),3),]
  ))
)

给出

> out
# A tibble: 5 x 4
  cut        freq distance nearest
  <chr>     <dbl>    <dbl> <named list>
1 Very Good   403     14   <tibble [3 x 3]>
2 Fair       3920     25   <tibble [3 x 3]>
3 Premium    5938      5   <tibble [3 x 3]>
4 Good       4593     40   <tibble [3 x 3]>
5 Ideal     21551      2.4 <tibble [3 x 3]>

其中

> out$nearest
$`1`
        cut  freq distance
1      Fair  1610        4
5     Ideal 21551       34
3 Very Good 12082       45

$`2`
        cut  freq distance
5     Ideal 21551       34
3 Very Good 12082       45
1      Fair  1610        4

$`3`
        cut  freq distance
1      Fair  1610        4
5     Ideal 21551       34
3 Very Good 12082       45

$`4`
        cut  freq distance
3 Very Good 12082       45
5     Ideal 21551       34
4   Premium 13791       50

$`5`
        cut  freq distance
1      Fair  1610        4
5     Ideal 21551       34
3 Very Good 12082       45

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...