From ripley at stats.ox.ac.uk  Tue Sep  7 10:16:51 2004
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue Sep  7 10:16:56 2004
Subject: [Rd] Inconsistencies in subassignment  (PR#7210)
In-Reply-To: <20040903234728.CB4B910EB7@slim.kubism.ku.dk>
Message-ID: <Pine.LNX.4.44.0409070908110.8491-100000@gannet.stats>

On Sat, 4 Sep 2004 tlumley@u.washington.edu wrote:
 
> I have made the 3-d case do the same as the vector case, which is what the
> C code clearly intended (a goto label was in the wrong place).
> 
> This leaves the bigger question of the right thing to do. I note that data
> frames give an error when any indices are NA.

One case is unambiguous and common:

	x[ind] <- val

where `val' is of length one.  I've written code to ban all other 
subassignments involving NAs.  Once I fixed occurrences in R itself 
(notably in ifelse), only three problems remain in tests over the CRAN 
packages

< Running examples in ape-Ex.R failed.
< > ### * popsize
    area[a == 0] <- stepfunction[a == 0]

< Running examples in RandomFields-Ex.R failed.
< > ### * ShowModels
expr[pmatch(covlist, namen)] <- exprlist

< Running examples in sm-Ex.R failed.
< > ### * sm.sphere
    z[xyzok < 0] <- (za - zb)[xyzok < 0]

The first and third are a typical usage, where R makes more sense than S.
[Worryingly, sm was written for S-PLUS and would seem to be incorrect 
there.]

So in R 2.0.0 we will have

\section{NAs in indexing}{
  When subscripting, a numerical, logical or character \code{NA} picks
  an unknown element and so returns \code{NA} in the corresponding
  element of a logical, integer, numeric, complex or character result,
  and \code{NULL} for a list.

  When replacing (that is using subscripting on the lhs of an
  assignment) \code{NA} does not select any element to be replaced.  As
  there is ambiguity as to whether an element of the rhs should
  be used or not (and \R handled this inconsistently prior to \R 2.0.0),
  this is only allowed if the rhs value is of length one (so the two
  interpretations would have the same outcome).  
}

-- 
Brian D. Ripley,                  ripley@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


[Rd] Inconsistencies in subassignment (PR#7210)
Prof Brian Ripley