I am pretty new to R. I Was trying hands-on the titanic dataset (available online). I was running a code to impute the missing values in Age column. But I was getting an error - Error in if (class[i] == 1) { : missing value where TRUE/FALSE needed. Need some help on how to do away with the error. Below is the code used:
impute_Age <- function(Age, class){
vector <- Age
for(i in 1:length(Age)){
if (is.na(Age[i])){
if(class[i] == 1){
vector[i] <- round(mean(filter(titanic, titanic$ï..pclass==1)$age, na.rm=TRUE),0)
}else if (class[i] == 2){
vector[i] <- round(mean(filter(titanic, titanic$ï..pclass==2)$age, na.rm=TRUE),0)
}else{
vector[i] <- round(mean(filter(titanic, titanic$ï..pclass==3)$age, na.rm=TRUE),0)
}
}else{
vector[i]<-Age[i]
}
}
return(vector)
}
imputed_Age <- impute_Age(titanic$age, titanic$ï..pclass)
titanic$age <- imputed_Age