Home > Mobile >  Why ifelse() works different than if(){}else{} in R
Why ifelse() works different than if(){}else{} in R

Time:04-13

Does anyone know why the below code using if(){}else{} structure works:

if(T){
     y = 1
     }else{y = 0}

but the below one using ifelse raises an error:

> ifelse(T, y = 1, y = 0)
Error in ifelse(T, y = 1, y = 0) : 
  formal argument "yes" matched by multiple actual arguments

CodePudding user response:

You should use ifelse in this way (instead of the asignment within ifelse(...))

y <- ifelse(T,1,0)
  • Related