[R] Why does R replace all row values with NAs

peter dalgaard pdalgd at gmail.com
Sun Mar 1 14:05:07 CET 2015


> On 27 Feb 2015, at 16:02 , Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
> 
> Yes.  Indexing with a logical NA is probably a mistake, and this is one
> way to signal it without actually triggering a warning or error.

There are cases where it isn't (usually) a mistake, e.g. pch=c(25,24)[sex], where it is quite crucial that the result has the same length as the index (i.e., sex) and where it makes good sense to use an NA plotting character if sex is unknown.

For logical index, it is harder to come up with a good excuse for the NA behaviour, except that R's NA is by default logical so there would be trouble explaining differences between c(x[NA], x[1]) and x[c(NA, 1)].

(The annoyance of getting a data frame half-full of NA was the reason that subset() was written so that it removes rows corresponding to NA indices).

-pd 

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list