错误:缺少需要TRUE / FALSE的值

时间:2018-09-20 19:59:13

标签: r

我对R相当陌生。我正在尝试动手处理泰坦尼克号数据集(可在线获得)。我正在运行代码,以估算“年龄”列中的缺失值。但是我遇到一个错误-if(class [i] == 1){:需要TRUE / FALSE时缺少值。 需要一些有关如何消除该错误的帮助。 下面是使用的代码:

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

2 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

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)
}

答案 1 :(得分:0)

如果您想摆脱for-loops,可以使用嵌套的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)))
 }