This is just a simple question but really taking my time as I am new in R.
I'm trying to write a code for the variance of a portfolio.
For example, I have the following :-
weight=c(0.3,0.2,0.5)
cov = matrix( c(0.2, 0.4, 0.3, 0.4, 0.5, 0.3,0.3,0.3,0.4),nrow=3, ncol=3, byrow = TRUE)
for (i in 1:3){
for (j in 1:3) {
port = sum((weight[i]^2) * (cov[i,i]^2)) + sum(weight[i] *weight[j]* cov[i,j]) }}
The answer if I calculate manually should be 0.336. But R gave me port=0.12 which is wrong. Where is my mistake?

port <- port+sumand useport<-0before loop