The easiest answer is
m3<-m2
m3[lower.tri(m3)] <- NA
Or you can do
m3<-`[<-`(m2, lower.tri(m2), NA)
which looks very awkward but it works. You're basically intercepting the value that from []<-
what would normally replace the variable in the assignment and sending it to a new variable.\
And your strategy of m3 <- m2[lower.tri(m2)] <- NA
doesn't work because when you do a <- b <- c
that's the same as a <- (b <- c)
which is the same as a <-c; b<-c
because the assignment operator generally passes along what was on the right side of the equation. So you're just doing m3<-NA
. There is a special operator for matrices called [<-
that will reshape that NA
to an appropriate size for it's dimensions based on what you supply as an index. But here, you are not using []
on the m3
so you are not getting that special behavior