-1

I've got a matrix and now I search for the minimum value of EACH column. I've got de next code:

error.mars1[which(error.mars1==0)] = NA
minValue = min(error.mars1[,2],na.rm=T)

I've used NA because I want a nonzero minimum value. So this was to get the mean of column 2. But now I want the row number for which this is the minimum value. Can someone help me?

Silke
  • 177
  • 1
  • 11
  • You asked a variant the same question an hour ago and it was answered there ... – hjw Jun 03 '14 at 07:40
  • possible duplicate of [R: Get the row and column name of the minimum element of a matrix but with minimum != 0](http://stackoverflow.com/questions/24007875/r-get-the-row-and-column-name-of-the-minimum-element-of-a-matrix-but-with-minim) – 2Dee Jun 03 '14 at 07:52
  • Right, but then I've used min(which(error.mars1[,2] == minValue) – Silke Jun 03 '14 at 08:55
  • @Silke You used it as an example, but I think your question was fairly clear that you needed values for all columns not just the second one :) Thanks for changing your mind. I will delete my above comment as no longer relevant – Oleg Sklyar Jun 03 '14 at 08:57

2 Answers2

1

Generally what you are asking about is given by:

apply(error.mars1, 2, function(x) which(x == min(x, na.rm=TRUE)))

Alternatively:

apply(error.mars1, 2, which.min)
Vincent
  • 955
  • 2
  • 15
  • 32
Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62
  • The second element must be 2 for columns, and your second proposal gives the same result as the first (I tested). I edited your answer. – Vincent Jun 03 '14 at 07:47
  • Thanks. I just do not have R where I am now to test :) The second is just a short form for the first. – Oleg Sklyar Jun 03 '14 at 08:11
0

try:

which(error.mars1[,2] == minValue)

ThatGuy
  • 1,225
  • 10
  • 28