skip NA's when computing dot product
I am adjusting the measurements in a data matrix by subtracting their projections onto the first 1-2 principal components. The problem is, if there is even a single NA in the data matrix (almost inevitable for thousands of measurements), the inner product operation x%*%y (I also tried sum(x*y), for vectors x,y) returns NA. Is there a simple way (i.e. avoiding conditional statements and loops) of computing the inner product on the non-NA values, so that the operation actually returns something?
Incidentally, I would like to avoid just replacing NA's with 0's, since then I would have to renormalize the vectors at each stage.
You can try this command:
sum(x*y, na.rm = TRUE)