Supose you have a 3 by 3 matrix m with one or more zeros and you want to replace each zero by a given number.
The replacement must follow this rule:
If a zero is found on first column of m, then it should be replaced with the first element of vector v, if a zero is found on second column of m, then it should be replaced with the second element of vector v, and the same for the third column/vector element.
The matrix and the vector are:
m <- matrix(c(0,2,3,4,0,6,0,8,0), 3, 3, byrow = TRUE)
v <- c(1,5,9)
Of course, the length of v must match the number of columns in m. So the matrix is like this:
0 2 3
4 0 6
0 8 0
I want that whenever R finds a zero in the first column of m it replaces it by the first element of vector v, and so on, so that the final result would be:
1 2 3
4 5 6
1 8 9
I have done a lot of research and havent found a solution for this problem. Can someone please help?