From MSchwartz at mn.rr.com  Thu Oct 20 02:46:04 2005
From: MSchwartz at mn.rr.com (Marc Schwartz)
Date: Wed, 19 Oct 2005 19:46:04 -0500
Subject: [Rd] is.na<- problem
In-Reply-To: <XFMail.051020011342.Ted.Harding@nessie.mcc.ac.uk>
References: <XFMail.051020011342.Ted.Harding@nessie.mcc.ac.uk>
Message-ID: <1129769164.5955.5.camel@localhost.localdomain>

On Thu, 2005-10-20 at 01:13 +0100, Ted Harding wrote:
> On 19-Oct-05 Gabor Grothendieck wrote:
> > In the following the first element of xx should have
> > been set to 0 but remains NA.  Any comments?
> > 
> >> xx <- c(NA,1)
> >> is.na(xx) <- 0
> >> xx
> > [1] NA  1
> >> R.version.string # Windows XP
> > [1] "R version 2.2.0, 2005-09-20"
> 
> I wonder, has it ever worked? I get the same as you on
> 
> R.version.string #Linux
> [1] "R version 1.6.2, 2003-01-10"
> 
> R.version.string #Linux
> [1] "R version 1.8.0, 2003-10-08"
> 
> R.version.string #Linux
> [1] "R version 2.1.0, 2005-04-08"
> 
> with the exception of
> 
> R.version.string #Linux
> [1] "R version 1.2.3, 2001-04-26"
> 
> which does know about is.na()<- at all.
> 
> 
> Hmmm
> Ted.

According to the documentation in ?is.na:

   The generic function is.na<- sets elements to NA.

and in the details section:

Function is.na<- may provide a safer way to set missingness. It behaves
differently for factors, for example.


In reviewing the Green Book on the top of page 143, it shows an example
in which the RHS of the assignment are the indices into the LHS object
which are to be set to NA. For example:

> xx <- c(0:5)

> xx
[1] 0 1 2 3 4 5

> is.na(xx) <- c(3, 4)

> xx
[1]  0  1 NA NA  4  5


Thus, back to Gabor's example, the proper approach would of course be:

> xx <- c(NA,1)

> xx
[1] NA  1

> xx[is.na(xx)] <- 0

> xx
[1] 0 1


HTH,

Marc Schwartz



[Rd] is.na<- problem
Marc Schwartz