I am fairly new to R and I have a data frame with a lot of NA values. I am trying to replace those NA's with the mean of the column. However, some of my columns are factors and some are strings. I have attempted to use a for function that replaces the NA's with the mean if the column is a factor, and the string "other" if not. Here is what I have used:
for(i in 1:ncol(beer_data_train)){
beer_data_train[is.na(beer_data_train[,i]), i] <- (ifelse(is.factor(beer_data_train[,i])==TRUE, mean(beer_data_train[,i], na.rm=TRUE), "Other"))
}
I am still getting the following warnings:
Warning messages:
1: In mean.default(beer_data_train[, i], na.rm = TRUE) :
argument is not numeric or logical: returning NA
2: In mean.default(beer_data_train[, i], na.rm = TRUE) :
argument is not numeric or logical: returning NA
3: In mean.default(beer_data_train[, i], na.rm = TRUE) :
argument is not numeric or logical: returning NA
4: In mean.default(beer_data_train[, i], na.rm = TRUE) :
argument is not numeric or logical: returning NA
Any advice on how to fix this?
Aucun commentaire:
Enregistrer un commentaire