0

I fairly new to R and I'm trying to wrap my head around this error. I've already consulted other questions regarding it and all of them imply that the error is thrown when there is a vector/list in the condition of an if statement, but that's not my case. I have only one condition:

if (dataset$scda == "I"){
    a=1
}else if (dataset$scda == "A"){
    a=2
}else{
    a=0
}

And it throws the error

Error in if (dataset$scda == "I") { : 
  the condition has length > 1
Execution halted

I've tried to find out if the problem is caused by a vector in the condition, however, the output of

print(class(dataset$scda)) is [1] "character"

So the standard answer I've found so far (e.g. "you have a vector in the condition") doesn't apply.

  • 2
    It does apply :) A vector is when `length(...)` is > 1 (as error states), a scalar is when `length(...)` is 1. In your case check `length(dataset$scda)` . `if` is used with scalars and `ifelse` with vectors. – Ronak Shah Dec 15 '22 at 09:36
  • you are right! i don't know why it didn't cross my mind to check the length, I just assumed it was a character, silly me. Thank you! – succulentLover Dec 15 '22 at 09:48

0 Answers0