[Rd] is.na<- problem

Marc Schwartz MSchwartz at mn.rr.com
Thu Oct 20 02:46:04 CEST 2005


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



More information about the R-devel mailing list