Skip to content

question about if else

1 message · Liaw, Andy

#
You need to (re-)read ?ifelse.  In ifelse(L, v1, v2), L is suppose to be a
vector of logicals (or an expression that evaluates to one), and v1 and v2
are vectors of same length as L; i.e., ifelse() vectorizes if ... else ....
In the first case:

  r = ifelse(length(c())!=0, c(), c(1,2)) 

length(c()) != 0 is FALSE, so you just get 1 as the answer.  In the second
case,

  r = ifelse(length(c())==0, c(), c(1,2))

length(c()) == 0 is TRUE, so ifelse tries to return the first element of
c(), which does not exist.

I suspect you really want if ... else ....

Andy
------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}