[R] zero replacement

Robert Gentleman rgentlem at jimmy.harvard.edu
Tue Aug 3 18:22:53 CEST 1999


On Mon, Aug 02, 1999 at 03:14:43PM -0700, Michael Camann wrote:
> AARRGGHH!  Sometimes it's the simple things that are particularly frustrating, 
> especially late at night....
> 
> Can anyone suggest a simple means for replacing all of the zero values in a 
> matrix with NANs?  I ended up writing an awk script to massage the input file, 
> which works, of course, but is rather an inelegant blunt instrument.  I'd 
> prefer an R operation.  I'm certain that I'm missing something obvious here....
> 
> --Mike
>

  I might be missing something, but why not:

  x[x==0] <- NaN

  A small test example:

> x<-matrix(0,nr=3,nc=3)
> x
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
> x[x==0] <- NaN
> x
     [,1] [,2] [,3]
[1,]  NaN  NaN  NaN
[2,]  NaN  NaN  NaN
[3,]  NaN  NaN  NaN
> is.nan(x)
     [,1] [,2] [,3]
[1,] TRUE TRUE TRUE
[2,] TRUE TRUE TRUE
[3,] TRUE TRUE TRUE

 
-- 
+-------------------------------------------------------------------------+
| Robert Gentleman              voice: (617) 632-5045                     |
| Senior Lecturer               fax:   (617) 632-2444                     |
| University of Auckland	email1: rgentlem at jimmy.harvard.edu        |
| on leave at:                                                            |
| Dana-Farber Cancer Institute  email2: rgentlem at stat.auckland.ac.nz      |
+-------------------------------------------------------------------------+
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list