Q:

Argument of length zero in R after if function

I am getting an argument of length zero after running the code below. I have looked at other users with this issue, however I am unable to figure out the error.
gi <- pm%*%t(pm); #creates a 5k by 5k matrix by multiplying matrix by its transpose
dim(gi); # checks dimensions of matrix
ngen <- dim(pm)[1];
select <- matrix(F,ncol=ngen,nrow=ngen);
for(i in 1:(ngen-1)){
    for(ii in (i+1):ngen){
        if(gi[i,ii] < llim){
            next;
        }
        hit1 <- p.sum[i];
        hit2 <- p.sum[ii];
        if(gi[i,ii] >= qmat[hit1,hit2]){
            select[i,ii] <- TRUE;
        }
    }
}

This code returns the error: 
Error in if (gi[i, ii] >= qmat[hit1, hit2]) { : 
  argument is of length zero

I have a feeling that the problem is the gi variable as hit1 and hit2 return values, any ideas how I could fix it? Any assistance would be greatly appreciated.

A:

The most likely reason for the failure is that hit1 or hit2 returns 0.
m <- matrix(1:12,4)
m[0,3] # integer(0)
if (m[0,3] > 0) 'foo!'
#Error in if (m[0, 3] > 0) "foo!" : argument is of length zero

...And you can use tcrossprod(pm) instead of pm%*%t(pm) - it's faster and (in my opinion) clearer.

