[Rd] .. var() fails on NA in R 2.7.2 but not R 2.6.1
Martin Maechler
maechler at stat.math.ethz.ch
Sat Sep 13 16:29:03 CEST 2008
Hi,
I'm getting back to this topic, from a slightly different angle:
>>>>> "HenricN" == Henric Nilsson (Public) <nilsson.henric at gmail.com>
>>>>> on Sat, 06 Sep 2008 03:47:06 +0200 writes:
HenricN> Andrew Piskorski wrote:
>> I recently started using R 2.7.2, and noticed a surprising change in
>> the behavior of var() on NA data:
>>
>> R 2.6.1 (Patched), 2007-11-26, svn.rev 43541, x86_64-unknown-linux-gnu:
>>
>> > var(rep(NA,3))
>> [1] NA
[.................]
After some R-core internal discussion,
I have implemented the following two changes in
R-devel {to be R 2.8.0 soon}, and I'm hereby "RFCing" i.e.,
requesting for (your) comments:
1) cov() & cor() gain a new option 'use = "everything"',
*and* that is the new default in the cases where "all.obs" has been the
default, i.e. including the default uses of var() or sd()
{Andy had stdev(), but that's not a standard R function}.
Explicit use of use = "all.obs" (or other previously existing
options of 'use') will remain unchanged of course.
The new implementation (of use = "everything"), as committed to R-devel
a few days ago, basically replaces all error messages
"missing observations in cov/cor" (and *only* those, at the moment!)
by returning NA in the corresponding places
(many, if we have a cov/var/cor *matrix*).
2) cov() & cor() gain a new option 'use = "na.or.complete"',
{better proposals for this "na.or.complete" are still
welcome, but please think twice: we have partial matching...}
*and* that is the new default in the cases where "complete.obs" has been the
default, notably for var(*, na.rm=TRUE).
Consequently, all of
var(double(0))
var(double(0), na.rm=TRUE)
and var(NA, na.rm=TRUE)
now return NA instead of an error
{ "empty 'x'" or "no complete element pairs" }
This will entail that
i. {empty vector <==> {vector of all NAs (= empty after na.omit)}
ii. var(<empty>) |-> NA
by default.
----
Feedback is very welcome!
Martin Maechler, ETH Zurich and R core team.
More information about the R-devel
mailing list