Skip to content

Instantly share code, notes, and snippets.

@awhstin
Last active July 26, 2017 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awhstin/3ac1a3592daf22c0ade5f9fe167d5b62 to your computer and use it in GitHub Desktop.
Save awhstin/3ac1a3592daf22c0ade5f9fe167d5b62 to your computer and use it in GitHub Desktop.
simple function to return the column name with the max (or min) value from a comparison of columns.
data(mtcars)
col_compare<-function(df,cols,fun=which.max){
x<-data.frame(df)
temp<-x[,c(cols)]
wins<-colnames(temp)[apply(temp,1,fun)]
return(wins)
}
mtcars$max<-col_compare(mtcars,3:5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment