[R] stopifnot with logical(0)

Duncan Murdoch murdoch.duncan at gmail.com
Mon Dec 14 18:40:58 CET 2015

On 14/12/2015 11:45 AM, Hadley Wickham wrote:
> >> I wonder if R is missing an equality operator for this case. Currently:
> >>
> >> * == is suboptimal because it's vectorised
> >> * all.equal is suboptimal because it returns TRUE or a text string
> >> * identical is suboptimal because it doesn't do common coercions
> >>
> >> Do we need another function (equals()?) that uses the same coercion
> >> rules as == but isn't vectorised? (Like == it would only work with
> >> vectors, so you'd still need identical() for (e.g.) comparing
> >> environments)
> >
> > I don't think so.  We already have all(), so all(x == y) would do what you
> > want.
> But that recycles, which is what we're trying to avoid here.

I think this is too special a case to need a function.  Usually all(x == 
y) is the test you want, because so many R functions will recycle.  It's 
not so when x is the dim of an array.  So I could see a weak argument 
for an equalDim() function, but I think it's better just to use

stopifnot(length(dim) == 2, all(dim == c(3,4)))

I know the all() in the second arg isn't needed, but I think it makes 
the intention clearer.  I think there would be less confusion if 
stopifnot() required its args to be single logical values, so I usually 
try to use it that way.

Duncan Murdoch

More information about the R-help mailing list