Skip to content
Prev 274186 / 398506 Next

Mean or mode imputation fro missing values

Hi
var goes from 1 to 3, above you actually use df_test$1 which is not what 
you intend.
you shall use [] selection operator. However your Mode function does not 
correctly assign values 

for (var in 1:ncol(df_test)) {
                 if (class(df_test[,var])=="numeric") {
                                 df_test[is.na(df_test[,var]), var] <- 
mean(df_test[,var], na.rm = TRUE)
                                 } else if 
(class(df_test[,var])=="character") {
                                 Mode(df_test[is.na(df_test[,var]),var], 
na.rm = TRUE)
                                 }
}

Warning message:
In max(xtab) : no non-missing arguments to max; returning -Inf

You shall use debug(Mode] to see what is going on. I have no time to 
inspect it and do not see any obvious flaw.

Regards
Petr
http://www.R-project.org/posting-guide.html
http://www.R-project.org/posting-guide.html