Firebolt
Firebolt

Reputation: 43

how to perform a conditional rowsums by choosing the highest value of 2 columns

I'm new to R and I was looking for a way to sum values of entire rows while using the higher value between two specific columns.

for example,

set.seed(42)
df <- data.frame(a = sample(0:20,5),
             b = sample(0:20,5),
             c = sample(0:20,5),
             d = sample(0:20,5),
             e = sample(0:20,5),
             f = sample(0:20,5),
             g = sample(0:20,5))
print(df)

   a  b  c  d  e  f  g
1 19 10  9 19 18 10 15
2 18 14 14 20  2  7 16
3  5  2 17  2 20 17  7
4 14 11  4  8 17  8 12
5 10 17  7  9  1 14  0

now what I wanted is calculate for each row

a+b+c+(whichever higher between d and e)+(whichever higher between f and g)

I'm familiar with rowSums, but I don't know how to add the above mentioned condition

thanks in advance

Upvotes: 3

Views: 55

Answers (2)

Khaynes
Khaynes

Reputation: 1986

A data.table method:

library(data.table)
setDT(df)[, new := a + b + c + pmax(d, e) + pmax(f, g)]

Upvotes: 1

akrun
akrun

Reputation: 887223

We can use pmax to get the max value for the subset of columns per row and add with the rest

library(dplyr)
df %>% 
   mutate(new = a + b + c + pmax(d, e) + pmax(f, g))

Upvotes: 1

Related Questions