Hugo HK
Hugo HK

Reputation: 63

Compare three columns and create a new one in R data frame

I have a data set that looks like this:

ID  A       B       C
1   150529  148914  60277
2   328122  330293  531977
3   74310   78129   34038
4   97520   104695  55268

I'm trying to evaluate each row and identify the highest number and create a new column in this data set with the answer so I can proceed to aggregate later based on that.

I've created a function that looks like this:

winner <-function(a, b, c){
  if (a>b & a>c) 
    {return("blue")} 
  else if(b>a & b>c) 
    {return("red")}
  else 
    {return("yellow")}  
} 

The function works if I run it in the command line but if use it in the form of:

res <- transform(res, newcol=winner(PAN,PRI,PRD))

I get an error like the one below and the newcol gets the value "blue" for all rows:

Warning message:
In if (a > b & a > c) { :
  the condition has length > 1 and only the first element will be used

Upvotes: 2

Views: 1521

Answers (2)

Hugo HK
Hugo HK

Reputation: 63

Thanks to all, @alexis_laz gave the exact solution to my problem.

I have changed my code to:

res <- transform(res, newcol=c("blue", "red", "yellow")[max.col(res[-1])])

and it worked as expected giving the right results. Thanks again to all!

Upvotes: 4

Colonel Beauvel
Colonel Beauvel

Reputation: 31171

Try:

transform(res, newcol=apply(res[-1],1,max))
#  ID      A      B      C newcol
#1  1 150529 148914  60277 150529
#2  2 328122 330293 531977 531977
#3  3  74310  78129  34038  78129
#4  4  97520 104695  55268 104695

This solution is also faster than apply:

transform(res, newcol=do.call(pmax, res[-1]))

Upvotes: 3

Related Questions