[R] Replace the missing values with column mean values?

arun smartpink111 at yahoo.com
Mon May 6 16:17:00 CEST 2013


set.seed(25)
dat1<- as.data.frame(matrix(sample(c(1:20,NA),10*20,replace=TRUE),ncol=20))
dat2<- dat1
vec1<-colMeans(dat1,na.rm=TRUE)
 dat2[]<-lapply(seq_len(ncol(dat1)),function(i) {x<-dat1[,i]; x[is.na(x)]<- vec1[i];x})
dat2<-signif(dat2,digits=3)
 dat2


A.K.


>I have Data frame with 20 variables. Each variable contains some missing  values. So I want to replace the missing values by the column means. 
>Please reply me. Thanks in advance....



More information about the R-help mailing list