Home > Back-end >  warning: the condition has length >1
warning: the condition has length >1

Time:10-26

I am trying to write a for loop with an ifelse statement for my project. I keep getting this error: "the condition has length > 1", and I am unsure why this keeps popping up.

Here is my code:

scanning.schools<- vector(mode="character",nrow(valid))
for ( i in seq_len(nrow(school.safety))){
  if (scanning== "Full-Time" & scanning.incident == "Y"){
    scanning.schools[i]<- "Scanning Caught Crime"
    } else if (scanning == "Full Time" & scanning.incident=="N"){
   scanning.schools[i]<- "Crime Occurred after Scanning"
    }
  }

Thanks so much!

CodePudding user response:

Try this:

scanning.schools<- vector(mode="character",nrow(valid))
for ( i in seq_len(nrow(school.safety))){
  if (scanning[i]== "Full-Time" & scanning.incident[i] == "Y"){
    scanning.schools[i]<- "Scanning Caught Crime"
    } else if (scanning[i] == "Full Time" & scanning.incident[i]=="N"){
   scanning.schools[i]<- "Crime Occurred after Scanning"
    }
  }
  • Related