Last active
July 26, 2017 16:42
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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