This should be fairly easy but I'm just confusing myself. With data frame:
df <- data.frame(id = 1:5, rate1 = c(1, 0, 0.3, 0.5, 1)
, rate2 = c(0, 0.6, 1, 0, 1), cost1 = 3:7, cost2 = 8:4)
I want to add a new column according to whether rate1 or rate2 is equal to 1 then fill in the new column with maximum value of cost1 and cost2 otherwise use 0. The desired result will be:
  id rate1 rate2 cost1 cost2 overspent
1  1   1.0   0.0     3     8         8
2  2   0.0   0.6     4     7         0
3  3   0.3   1.0     5     6         6
4  4   0.5   0.0     6     5         0
5  5   1.0   1.0     7     4         7
I assume using something similar to:
df$overspent <- with(df, ifelse((rate1 == 1 || rate2 == 1)
                     , apply(cbind(cost1, cost2), 1, max), 0))
but apparently it does not work. Please correct me. Thanks!
 
    