Finding the column number of the smallest element in a certain row

有些话、适合烂在心里 提交于 2019-11-29 16:30:54
user20650

If there is only a single minimum for each row you can find it with

apply(C, 1, which.min)

or (from R: finding column with minimum value in each row when there is a tied). See ?max.col for more options.

max.col(-C, "first")

edit (thanks to @flodel in the comments)

You can do this for individual rows by

which.min(C[1,])

Or if there are multiple matches

apply(C, 1, function(i) which(i == min(i)))

You get 5, as -7 is the fifth element of the matrix as it goes column wise. Look at c(C)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!