Hi @Zakky,
If I understand your question, you want to update X[i] to 0 every time the corresponding Z[i] == 0, otherwise leave X[i] as is. Is this correct? Secondly, is using loops and mapply a requirement?
The easiest and fastest approach is to take advantages of R's vectorization.
You can test the equality of Z == 0, which returns a logical vector/matrix
where the condition is either true or false.
Z == 0
[,1]
[1,] FALSE
[2,] FALSE
[3,] TRUE
[4,] FALSE
[5,] FALSE
[6,] FALSE
[7,] FALSE
[8,] TRUE
[9,] FALSE
[10,] FALSE
[11,] FALSE
[12,] TRUE
[13,] FALSE
[14,] TRUE
[15,] FALSE
[16,] FALSE`
From here, you can use this test of equality as a way to index the positions of X and update
it where the Z == 0 is TRUE.
X[Z == 0] <- 0
giving
X
[,1]
[1,] 2.0
[2,] 4.5
[3,] 0.0
[4,] 9.5
[5,] 12.0
[6,] 14.5
[7,] 17.0
[8,] 0.0
[9,] 22.0
[10,] 24.5
[11,] 27.0
[12,] 0.0
[13,] 32.0
[14,] 0.0
[15,] 37.0
[16,] 39.5