5

What is the opposite function of max.col (R language).

I couldn't find min.col or relevant info in the manual:

https://stat.ethz.ch/R-manual/R-devel/library/base/html/maxCol.html

I'm trying to generate an opposite function of this:

cols = which(names(df) %in% c( "avp", "USD.KRW","cd")); 
df$max_col = names(df)[cols][max.col(df[cols])]
Tamara Koliada
  • 1,200
  • 2
  • 14
  • 31
  • Must be something close to `which.min` but not sure. Try `apply(df,1,which.min)` – NelsonGon Feb 17 '19 at 06:09
  • Possible duplicate of [Get the row and column name of the minimum element of a matrix](https://stackoverflow.com/questions/4207833/get-the-row-and-column-name-of-the-minimum-element-of-a-matrix) – Prathamesh Doke Feb 17 '19 at 06:10
  • What is `cols` in your example? @Renaissance please read [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and edit your question accordingly. – NelsonGon Feb 17 '19 at 06:16
  • cols = which(names(df) %in% c( "avp", "USD.KRW","cd")); df$max_col = names(df)[cols][max.col(df[cols])] – Renaissance Feb 17 '19 at 06:18

1 Answers1

14

You can use

min.col <- function(m, ...) max.col(-m, ...)

Used in formal optimization (where many tools prefer to find a minimum of a function), the opposite of a maximal number is the minimal negative number.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Renaissance, does this answer your question? If yes, please "accept" it. If not, I suggest you edit your question, otherwise you are less likely to get a better answer. – r2evans Feb 21 '19 at 01:42