0
votes

I have the following data set:

A_1 <- c(2, 3, 3, 2, 4)
A_2 <- c(5, 5, 6, 4, 4)
A_3 <- c(9, 9, 8, 7, 6)
B_1 <- c(1, 0, 0, 0, 0)
B_2 <- c(1, 1, 1, 0, 0)
B_3 <- c(0, 0, 0, 1, 1)
df <- cbind(A_1, A_2, A_3, B_1, B_2, B_3)
> df
     A_1 A_2 A_3 B_1 B_2 B_3
[1,]   2   5   9   1   1   0
[2,]   3   5   9   0   1   0
[3,]   3   6   8   0   1   0
[4,]   2   4   7   0   0   1
[5,]   4   4   6   0   0   1

I would like to replace the values in A_1 with NAs in rows where B_1 == 1, I would like to replace the values in A_2 with NAs in rows where B_2 == 1, and I would like to replace the values in A_3 with NAs in rows where B_3 == 1. The final data set should look like the following:

> df
     A_1 A_2 A_3 B_1 B_2 B_3
[1,]  NA  NA   9   1   1   0
[2,]   3  NA   9   0   1   0
[3,]   3  NA   8   0   1   0
[4,]   2   4  NA   0   0   1
[5,]   4   4  NA   0   0   1

What is the most efficient way to do this for a large data set? Thanks!

1

1 Answers

2
votes

How about something like this...

df[,1:3][df[,4:6]==1] <- NA

df
     A_1 A_2 A_3 B_1 B_2 B_3
[1,]  NA  NA   9   1   1   0
[2,]   3  NA   9   0   1   0
[3,]   3  NA   8   0   1   0
[4,]   2   4  NA   0   0   1
[5,]   4   4  NA   0   0   1