Skip to content
Prev 17474 / 398513 Next

replace NAs

With this function you may replace the NA with the mean or median of the non
missing values

##   replace NA
##
rep.na<-function(x, my.mean=TRUE)
{
    if (!my.mean){valore<-median(x[!is.na(x)])}
    else {valore<-mean(x[!is.na(x)])}
    for (i in (1:length(x))){if (is.na(x[i])==TRUE) {x[i]<-valore}}
    x<<-x
}
##
##
i.e.
[1] NA 12 NA 14 15 17 21
[1] 15.8 12.0 15.8 14.0 15.0 17.0 21.0
[1] 15 12 15 14 15 17 21

Good job, isaia.
juan pablo perez wrote:

            
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Ennio D. Isaia
~ Dep. of Statistics & Mathematics, University of Torino
~ Piazza Arbarello, 8 - 10128 Torino (Italy)
~ Phone: +39 011 670 62 51 ~~ Fax: +39 011 670 62 39
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._