[Rd] Suggestion: make if (x) an error also for length(x) > 1

Henrik Bengtsson hb at stat.berkeley.edu
Tue Aug 3 14:11:21 CEST 2010


For a while, R has been generating a warning for if (x) when length(x)
> 1.  With length(x) == 0, we get an error.

> x <- logical(0)
> if (x) 1;
Error in if (x) 1 : argument is of length zero
> x <- logical(1)
> if (x) 1;
> x <- logical(2)
> if (x) 1;
Warning message:
In if (x) 1 :
  the condition has length > 1 and only the first element will be used

Time to make the length(x) > 1 case an error to?  Is there any sane
programming reason why it should work as if (x[1]) with a warning?
How many bugs propagate because of this?

/Henrik



More information about the R-devel mailing list