0

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 
user2543622
  • 5,760
  • 25
  • 91
  • 159
  • 2
    Try with `sort` i.e. `apply(x, 1, function(x) names(sort(x)[2]))` – akrun Apr 22 '17 at 15:48
  • 1
    Have a look [1](http://stackoverflow.com/questions/2453326/fastest-way-to-find-second-third-highest-lowest-value-in-vector-or-column) & [2](http://stackoverflow.com/questions/5569038/fastest-way-to-find-the-index-of-the-second-third-highest-lowest-value-in) – user2100721 Apr 22 '17 at 15:50

3 Answers3

1

Just this:

colnames(x)[apply(x, 1, order)]
#[1] "c" "a" "b" "d" "e"
989
  • 12,579
  • 5
  • 31
  • 53
1

You can use the below function
Here n is number of nth minimum value

Step1: Picking the nth value. Since the data is sorted in ascending order therefore, nth minimum value.
Step2: Compare all the values with the nth minimum value and pick the corresponding column of the nth minimum value
arr.value = 'TRUE' shows the value of row number and column number where the condition is true.

x = t( data.frame(c(11,12,1,14,15)) )
colnames(x)=c('a','b','c','d','e')  

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)
}


min(x,1) # first minimum value and corresponding column name 
min(x,2) # second minimum value and corresponding column name
....  

Output:
enter image description here Let me know in case of any queries.

G.Arima
  • 1,171
  • 1
  • 6
  • 13
  • how to get column index? – user2543622 Apr 22 '17 at 17:01
  • I have incorporated that in the query. Check it out and let me know if you still have any doubts. – G.Arima Apr 22 '17 at 17:20
  • i tried to use `min(myans[,c(0:5)],3)` where myans has first 5 columns from where i want to find minimums, but i got an error ` Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = decreasing)) : undefined columns selected ` – user2543622 Apr 22 '17 at 17:31
  • "min(myans,3)" will serve the purpose because column name has already been evaluated in the function. Check out the code. – G.Arima Apr 22 '17 at 17:38
  • I have attach a picture as output? It is working perfectly fine for me. Check if you have written exactly the same query. – G.Arima Apr 22 '17 at 17:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142356/discussion-between-g-arima-and-user2543622). – G.Arima Apr 22 '17 at 18:12
1

as commented by akrun below works. Below code will give 2nd minimum...

minimum2=apply(dataframe_name, 1, function(x) (sort(x))[2])
index2=apply(dataframe_name, 1, function(x) names(sort(x)[2]))
user2543622
  • 5,760
  • 25
  • 91
  • 159