I know this is a common question, but the common answer of r[order(unlist(r))]
isn't working due to numerical keys. I can't find any answers after a while of searching. I did see How can I get top n values with its index in R? but that uses a dataframe, and I don't know how to implement a dataframe into my function.
I have a list of values which I created by using r[[i]] <- value
within a loop. Now I want to sort that list, but keep the numerical key.
> r
[[1]]
[1] 4396750
[[2]]
[1] 6340260
[[3]]
[1] 8449058
[[4]]
[1] 5676146
> r <- r[order(unlist(r))]
> r
[[1]]
[1] 4396750
[[2]]
[1] 5676146
[[3]]
[1] 6340260
[[4]]
[1] 8449058
I need some way to retain the keys, so that I know the values, from lowest to highest, are represented by keys 1,4,2,3
. Then I will need to keep the keys with the lowest 3 values. So my end result should be some list or vector that gives me 1,4,2
.
My example is simplified, so doing some shortcut like "just drop the highest value" won't work.
The actual code for the function is here:
knnacc <- function(e){
dist = list()
for(j in 1:10000) {
dist[[j]] <- distance(TEST[["image"]][[e]], TRAIN[["image"]][[j]])
}
#dist <- dist[order(unlist(dist))]
return(dist)
}
I would like to reach a point where the function returns the 20 keys associated with lowest distance out of 60,000.