Skip to content
Prev 17424 / 398513 Next

replace NAs

Juan,

Replacing NAs is relatively easy. Nonetheless, I wrote some simple code 
to make it even easier for me.  You might find it useful.  The first 
replaces NAs with the mean, the second replaces NAs with a value you 
specify.  This works for a single vector, you can also use the apply() 
function to use it across multiple columns in a data frame or matrix.

If anyone is interested, I also have code to produce least squares means 
by levels of a factor given any number of covariates and code for 
classical item analysis (reliability and item characteristics).  

replace.na.m<-
  function (x){
    X<-mean(x,na.rm=TRUE)
    ifelse ( is.na(x)=="TRUE",X,x)
}


replace.na.x<-
  function(x, value){
    ifelse (is.na(x)=="TRUE", value , x)
}
juan pablo perez wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._