[R] R: R: [Re:] function to replace missing values with median value?]]

Guazzetti Stefano Stefano.Guazzetti at ausl.re.it
Thu May 4 08:09:33 CEST 2006


oops!, I pressed the 'send' key too soon, ...

see ?replace

replace(x, is.na(x), median(x, na.rm=T))

take also a look at the function itself

> replace
function (x, list, values) 
{
    x[list] <- values
    x
}
<environment: namespace:base>

Stefano

   >-----Messaggio originale-----
   >Da: r-help-bounces at stat.math.ethz.ch
   >[mailto:r-help-bounces at stat.math.ethz.ch]Per conto di 
   >Guazzetti Stefano
   >Inviato: Thursday, May 04, 2006 07:55 AM
   >A: isaia at econ.unito.it; ruser2006 at yahoo.com
   >Cc: r-help at stat.math.ethz.ch
   >Oggetto: [R] R: [Re:] function to replace missing values with median
   >value?]]
   >
   >
   >there is also a replace function
   >
   >
   >   >-----Messaggio originale-----
   >   >Da: r-help-bounces at stat.math.ethz.ch
   >   >[mailto:r-help-bounces at stat.math.ethz.ch]Per conto di
   >   >isaia at econ.unito.it
   >   >Inviato: Thursday, May 04, 2006 07:31 AM
   >   >A: ruser2006 at yahoo.com
   >   >Cc: r-help at stat.math.ethz.ch
   >   >Oggetto: [R] [Re:] function to replace missing values 
   >with median
   >   >value?]]
   >   >
   >   >
   >   >The following should work
   >   >
   >   >sz <- function(x) { ifelse(is.na(x) == F, x, median(x, 
   >   >na.rm=TRUE)) }
   >   >
   >   >best, isaia.
   >   >
   >   >-------- Original Message --------
   >   >Subject: [R] function to replace missing values with 
   >median value?
   >   >Date: Wed, 3 May 2006 10:06:40 -0700 (PDT)
   >   >From: r user <ruser2006 at yahoo.com>
   >   >To: rhelp <r-help at stat.math.ethz.ch>
   >   >
   >   >I have a data set with ~10 variables (i.e. columns).
   >   >
   >   >I wrote this little function to replace missing values
   >   >with zero.
   >   >
   >   >“ sz <- function(x) { ifelse(is.na(x)==F,x,0) } “
   >   >
   >   >Can anyone help with a function that replaces missing
   >   >values with the median of the non-missing values?
   >   >
   >   >______________________________________________
   >   >R-help at stat.math.ethz.ch mailing list
   >   >https://stat.ethz.ch/mailman/listinfo/r-help
   >   >PLEASE do read the posting guide!
   >   >http://www.R-project.org/posting-guide.html
   >   >
   >   >-- 
   >   >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   >   >~ Ennio D. Isaia
   >   >~ Dep. of Statistics & Applied Mathematics, University of Torino
   >   >~ Piazza Arbarello, 8 - 10122 Torino (Italy)
   >   >~ Phone: +39 011 670 57 29 ~~ Fax: +39 011 670 57 83
   >   >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   >   >
   >   >______________________________________________
   >   >R-help at stat.math.ethz.ch mailing list
   >   >https://stat.ethz.ch/mailman/listinfo/r-help
   >   >PLEASE do read the posting guide! 
   >   >http://www.R-project.org/posting-guide.html
   >   >
   >   >
   >
   >______________________________________________
   >R-help at stat.math.ethz.ch mailing list
   >https://stat.ethz.ch/mailman/listinfo/r-help
   >PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list