[R] replace NA's with row means for specific columns

Jim Lemon drjimlemon at gmail.com
Tue Nov 3 00:33:23 CET 2015


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]]



More information about the R-help mailing list