Rock
Rock

Reputation: 2967

Add new column with row wise maximum on multiple conditions of other columns

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!

Upvotes: 0

Views: 1058

Answers (1)

flodel
flodel

Reputation: 89057

You need to use |, not ||. But see how this is a bit more elegant:

transform(df, overspent = ifelse(rate1 == 1 | rate2 == 1,
                                 pmax(cost1, cost2), 0))

Also, as @Ferdinand.kraft points out, comparing numerics might be dangerous (not in this example though) so you might want to replace things like rate1 == 1 with:

abs(rate1 - 1) < .Machine$double.eps ^ 0.5

or

sapply(rate1 - 1, function(x)isTRUE(all.equal(x, 0)))

Upvotes: 2

Related Questions