I distill from your for
loop that you want to perform the same action on the combination of line 4 and line 5 and on the combination of line 4 and 6. I think you then best can make a generic function and apply it on both combinations. I propose the following solution for your problem.
get.code <- function(x) {
if(x[1] == 0 & x[2] == 0){0} else if (x[1] == 0 & x[2] == 1){3
}else if(x[1] == 1 & x[2] == 1){4} else {2}
}
code1 <- apply(m1[,c(4,5)], 1, get.code)
code2 <- apply(m1[,c(4,6)], 1, get.code)
cbind(m1,code1, code2)