I am not sure how to loop over each column to replace the NA values with the column mean. When I am trying to replace for one column using the following, it works well.
Column1[is.na(Column1)] <- round(mean(Column1, na.rm = TRUE))
The code for looping over columns is not working:
for(i in 1:ncol(data)){
data[i][is.na(data[i])] <- round(mean(data[i], na.rm = TRUE))
}
the values are not replaced. Can someone please help me with this?
mean(x) +rnorm(length(missing(x)))*sd(x). That will not take account of correlations between the missings (or the correlations of the measured), but at least it won't seriously inflate the significance of the results. Better would be to get experience with the packages that handle imputation of missing values. There are quite a few subtleties underlying the problem. - IRTFMmean(x)+rnorm(length(missing(x)))*sd(x)? When I run it, I getError in missing(x) : invalid use of 'missing'. I expect the intention was to take the mean of the available values for x, then add rnorm(length of NAs)*sd(available values for x). Correct? I loved the malpractice line :-). I'm personally looking for a quick hack because I'm working with the '98 KDD cup dataset that has 120+ attributes with NAs. I'd like to drop most of them, and the instructions are to exclude only >= .995 NA . . . - Daniel Fletcherrnorm( n=sum(is.na(x)) , mean=mean(x), sd=sd(x) )would be closer to working code. - IRTFM