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.... ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.