简体   繁体   English

从 R 中的矩阵获取第二个最小值

[英]get second minimum value from a matrix in R

I have a matrix with distances between points:我有一个点之间的距离矩阵:

> head(distances)
          2914      2915      2924     2953     2954     2988     2989      3183      3184     3341
317_1 11116.25 10534.133 12759.250 2418.834 8386.288 9123.886 9573.037  4396.882  9179.407 3420.459
317_2 10827.64 10003.469 12307.238 1996.878 7994.617 8543.298 9006.385  4795.352  9363.853 3804.140
317_3 13544.06 10036.287  7341.391 3079.408 2918.336 7298.468 8283.268  9861.902 14234.768 8875.232
317_4 13495.06  9859.046  7102.862 3271.923 2744.106 7081.206 8082.261 10058.709 14362.919 9069.278
317_5 14652.07 11234.219  7378.551 3642.721 2782.711 8468.613 9469.082 10257.441 14899.174 9292.621
317_6 14796.52 11265.946  7108.030 3893.178 2511.896 8458.536 9476.372 10530.603 15150.529 9564.130
      

I want to obtain the minimum value for each row and respective column name (it's the code for the points), but also the second minimum value.我想获得每行和相应列名的最小值(它是点的代码),但也是第二个最小值。

I managed to get the minimum values + column/row names perfectly using this code:我设法使用以下代码完美地获得了最小值+列/行名称:

result <- t(sapply(seq(nrow(distances)), function(z) {
      j <- which.min(distances[z,])
      c(rownames(distances)[z], colnames(distances)[j], distances[z,j])
    }))

 min_dist1 <- as.data.frame(result)

 colnames(min_dist1) <- c("cluster_id", "point_id", "min_dist") #min_dist is in meters 

How can I get another two columns, with the second minimum distance and also the respective point id?如何获得另外两列,第二个最小距离以及各自的点 id? I tried to incorporate this solution Find the second minimum value in R in the sapply() but didn't work well...我试图合并这个解决方案在 sapply() 中找到 R 中的第二个最小值但效果不佳......

Any suggestions?有什么建议么?

The solution would be:解决方案是:

result <- t(sapply(seq(nrow(distances)), function(z) {
      js <- order(distances[z,])[1:2]
      c(rownames(distances)[z], colnames(distances)[js[1]], distances[z,js[1]],  colnames(distances)[js[2]], distances[z,js[2]])
    }))
    
    min_dist <- as.data.frame(result)
    colnames(min_dist) <- c("cluster_id", "point_id1", "min_dist1", "point_id2", "min_dist2")
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM