I have code as below. It finds the minimum value in a row and column name where that minimum value is. How can i modify my code to get values of 2nd, 3rd, 4th, 5th minimums and respective column names?
x = t( data.frame(c(11,12,1,14,15)) )
colnames(x)=c('a','b','c','d','e')
minimum = apply(x, 1, min)
minimum
index = colnames(x)[apply(x, 1, which.min)]
index
-------------------------------update1
I tried below code. myans_a has 5 columns. But the code fails
#function to find second minimum and associated class name
min <- function(x,n) {
value = sort(x, FALSE)[n]
column_name = colnames(x)[which(x == value, arr.ind = TRUE)[2]]
paste0("Column:",column_name," , Value:",value)
}
myans_a=myans[,c(1:5)]
min(myans_a,3)
> min(myans_a,3)
Show Traceback
Rerun with Debug
Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = decreasing)) :
undefined columns selected