find the column with lowest value in r

2019-05-16 22:53发布

问题:

I have large size matrix and try to find the column that has the minimum value for each row. For instance, here is my matrix, (simply generate with matrix(sample(12),nrow = 3)). With the matrix I want to to have a vector (3,4,1) representing the column number which contains the lowest value in each row. How should I do it? It could be duplicated question but I could not find answers.

      [,1] [,2] [,3] [,4]
[1,]   10   11    1   12
[2,]    8    9    7    3
[3,]    2    5    6    4

回答1:

Use max.col:

max.col(-mat)
# [1] 3 4 1


标签: r matrix apply