[Rd] edge case concerning NA in dim() (PR#13729)
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri May 29 15:22:54 CEST 2009
On Fri, 29 May 2009, astokes at esica.com wrote:
> Full_Name: Allan Stokes
> Version: 28.1
> OS: XP
> Submission from: (NULL) (22.214.171.124)
> I'm trying to use package HDF5 and have discovered some round-trip errors: save,
> load, save is not idempotent. I started digging into the type system to figure
> out what type graffiti is fouling this up.
> Soon I discovered that comparisons with NULL produce zero length vectors, which
> I hadn't known was possible, and I started to wonder about the properties of
> zero length objects.
> L0 <- logical (0)
> dim(L0) <- c(0) # OK
> dim(L0) <- c(1) # error
> dim(L0) <- c(0,1) # OK
> dim(L0) <- c(0,-1) # OK
> dim(L0) <- c(0,3.14) # OK, c(0,3) results
> dim(L0) <- c(0,FALSE) # OK c(0,0) results
> dim(L0) <- c(0,NA) # OK
> dim(L0) <- c(1,NA) # error
> dim(L0) <- c(1,NA,NA) # OK, SURPRISE!!
> NA*NA is normally NA, but in the test for dim() assignment, it appears that
> NA*NA == 0, which is then allowed. If the list contains more than one NA
> elements, the product seems to evaluate to zero.
The calculation was done in C and failed to take NAs (and indeed
negative values) into account. So
> L <- logical(1)
> dim(L) <- c(1, -1, -1)
Thank you for the report, changed in R 2.9.0 patched. (Since the
representation of an integer NA is negative, a test for positivity
would have caught this.)
> I can see making a case for 0*NA == 0 in this context, but not for NA*NA == 0.
> As an aside, I'm not sure why 0*NA does not equal 0 in general evaluation,
> unless NA is considered to possibly represent +/-inf.
In fact NA as used here is logical but is coerced to a numeric NA, and
a 'missing' numeric could take any possible value including Inf, -Inf
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-devel