# a function to return a vector of residuals, with NAs where they belong. residuals.with.na <- function(x,y){ resids <- lm(y~x)$resid missing <- is.na(x+y) data.out <- rep(NA,length(missing)) cdi <- 1 # cdi=current data index for (i in 1:length(missing)){ if(!missing[i]){ data.out[i] <- resids[cdi] cdi <- cdi+1 } } return(data.out) }