[R] question about if else
Don MacQueen
macq at llnl.gov
Fri Feb 27 20:12:47 CET 2004
ifelse() has three arguments, named 'test', 'yes', and 'no'.
In both of your two examples, you gave it a test argument of length equal to 1.
That is, both
length(c())!=0
and
length(c())==0
are expressions which when evaluated have length equal to 1.
Therefore, the ifelse() function wants to return an object of length
1. So, it wants to return the first element of either the 'yes'
argument, or the 'no' argument, depending on whether test is true or
false. But c() has length zero, there is no first element available
to return. So you get an error message.
-Don
At 12:12 PM -0600 2/27/04, Svetlana Eden wrote:
>Today is a good day for asking question, I guess.
>
>> c()
>NULL
>>
>> length(c())==0
>[1] TRUE
>>
> > r = ifelse(length(c())!=0, c(), c(1,2)) ### OK
>> r = c() ### OK
>> r = ifelse(length(c())==0, c(), c(1,2)) ### why this is not OK (given
>> the previous two)?
>Error in "[<-"(`*tmp*`, test, value = rep(yes, length =
>length(ans))[test]) :
> incompatible types
>>
>> c() == NULL
>logical(0)
>>
>> r = ifelse(c()==NULL, c(), c(1,2)) ### why this line does not
>> r ### result in error -
>logical(0) ### 'c()==NULL' is not TRUE
>and not FALSE ?
>>
>>
>
>--
>Svetlana Eden Biostatistician II School of Medicine
> Department of Biostatistics Vanderbilt University
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
More information about the R-help
mailing list