Hi again, Small typo in line 5 - should be replace_NAs<-function(x,group_lab=c("A","B","C")) { for(lab in group_lab) { indices<-grep(lab,names(x),fixed=TRUE) na_indices<-is.na(x[indices]) if(any(na_indices)) x[indices][na_indices]<-rowMeans(x[indices],na.rm=TRUE) } return(x) } Jim [[alternative HTML version deleted]]