'Why ifelse() works different than if(){}else{} in R

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


Solution 1:[1]

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

y <- ifelse(T,1,0)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ThomasIsCoding