Adarsha Murthy
Adarsha Murthy

Reputation: 145

select column value based on boolean values multiple columns - R

My data looks like below,

df=data.frame("X1" = c(1, 0, 0), "X2" = c(0, 0, 1), "X3" = c(0, 1, 0),
           "T1" = c(21, 20, 15), "T2" = c(35, 16, 19), "T3" = c(22, 32, 16))

X1  X2  X3  T1  T2  T3
1   0   0   **21**  35  22
0   0   1   20  16  **32**
0   1   0   15  **19**  16

And am expecting output as below

X1  X2  X3  T
1   0   0   21
0   0   1   32
0   1   0   19

As you can see, from T1,T2 and T3 only those values are picked based on boolean values in X1,X2 and X3.

I wrote a silly code using for loop, looking for a best approach..

Upvotes: 1

Views: 400

Answers (2)

akrun
akrun

Reputation: 887731

We multiply the first three columns (binary columns) with the next three columns (0 * any value = 0) and get the pmax (as there is only one non-zero value per row) to create the 'T' column

cbind(df[1:3], T = do.call(pmax, df[1:3]* df[4:6]))
#  X1 X2 X3  T
#1  1  0  0 21
#2  0  0  1 32
#3  0  1  0 19

Upvotes: 5

rafaelc
rafaelc

Reputation: 59284

x = c("X1", "X2", "X3")
t = c("T1", "T2", "T3")
df[, "T"] = rowSums(df[, x] * df[, t])

Explanation:

when you multiply df[, x] * df[, t], you get the values you want:

>>> df[, x] * df[, t]
  X1 X2 X3
1 21  0  0
2  0  0 32
3  0 19  0

then just do rowSums to get the values

[1] 21 32 19

Upvotes: 3

Related Questions