[R] [FORGED] missing and replace

Rolf Turner r.turner at auckland.ac.nz
Thu Apr 27 03:16:32 CEST 2017


On 27/04/17 12:45, Val wrote:
> HI all,
>
> I have a data frame with three variables. Some of the variables do
> have missing values and I want to replace those missing values
> (1represented by NA) with the mean value of that variable. In this
> sample data,  variable z and y do have missing values. The mean value
> of y  and z are152. 25  and 359.5, respectively . I want replace those
> missing values  by the respective mean value ( rounded to the nearest
> whole number).
>
> DF1 <- read.table(header=TRUE, text='ID1 x y z
> 1  25  122    352
> 2  30  135    376
> 3  40   NA    350
> 4  26  157    NA
> 5  60  195    360')
> mean x= 36.2
> mean y=152.25
> mean z= 359.5
>
> output
> ID1  x  y  z
> 1   25 122   352
> 2   30 135   376
> 3   40 152   350
> 4   26 157   360
> 5   60 195   360

This is pretty basic.  You really ought to learn a bit more about R if 
you are going to use R.  That being said, try:

newDF1 <- as.data.frame(lapply(DF1,function(x){
                         x[is.na(x)] <- mean(x,na.rm=TRUE)
                         x}))

There may be sexier ways of accomplishing your goal, but this should work.

cheers,

Rolf Turner

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list