I am very new to R and programming in general, and I need some help. I want to make a function that multiplies all even numbers in a given matrix by 3, for instance in this matrix:
X = matrix(1:9,3,3)
I have tried the replace function like this:
Multiply = function(M) {
x=M*3
replace(M, M%%2==0, x)
}
which gives me a result I don't understand. Any help would be much appreciated!
We can create the function as
Multiply <- function(M){ i1 <- M%%2 == 0
M[i1] <- M[i1]*3
M }