简体   繁体   中英

Error: missing value where TRUE/FALSE needed

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

you can try this:

for (i in 1:3){
   titanic[which(is.na(titanic$age) & titanic$pclass==i),"age"] <-
   round(mean(titanic[which(titanic$pclass==i),"age"],na.rm=TRUE),digits=0)
}

If you'd like to get away from for-loops you can do this with a nested if-else .

titanic$age <- {
 age1 = round(mean(titanic$age[titanic$pclass == 1], na.rm = TRUE))
 age2 = round(mean(titanic$age[titanic$pclass == 2], na.rm = TRUE))
 age3 = round(mean(titanic$age[titanic$pclass == 3], na.rm = TRUE))
 ifelse(is.na(titanic$age) & titanic$pclass == 1, age1,
    ifelse(is.na(titanic$age) & titanic$pclass == 2, age2,
           ifelse(is.na(titanic$age) & titanic$pclass == 3, age3, titanic$age)))
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM