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? I tried to incorporate this solution Find the second minimum value in R in the sapply() but didn't work well...
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")