The same result occurs outside of the function:
> ifelse(FALSE, 1, c(1, 2))
[1] 1
The function ifelse
is designed for use with vectorised arguments. It tests the first element of arg1, and if true returns the first element of arg2, if false the first element of arg3. In this case it ignores the trailing elements of arg3 and returns only the first element, which is equivalent to the TRUE
value in this case, which is the confusing part. It is clearer what is going on with different arguments:
> ifelse(FALSE, 1, c(2, 3))
[1] 2
> ifelse(c(FALSE, FALSE), 1, c(2,3))
[1] 2 3
It is important to remember that everything (even length 1) is a vector in R, and that some functions deal with each element individually ('vectorised' functions) and some with the vector as a whole.