[R] ignoring zeros or converting to NA

markleeds at verizon.net markleeds at verizon.net
Thu Aug 14 19:54:55 CEST 2008


Hi: If I remember correctly, I think I gave you something like mat[mat 
== 0]<-NA. I think what you're doing below is pretty different from
that but I may not be understanding what you want ? Let me know if I can 
clarify more because my intention was not to guide you
into doing below.  Looping is generally not a good idea in R and often 
unnecessary. If you send me whatever I sent you, maybe I can fix it up 
for what
you want.

On Wed, Aug 13, 2008 at  4:39 PM, rcoder wrote:

> Thank you for all your replies.
>
> Borrowing an alternative solution kindly provided by Mark Leeds, I am 
> using
> a conditional statement to pass non-zero values to a holding matrix 
> that has
> cells initially set to NA. The code is as follows:
>
> ##Code Start
> mat_zeroless<-matrix(NA,5000,2000)	#generating holding matrix
> for (j in 1:5000)
> 	{
> for (k in 1:2000)
> {
> if(mat[j,k]!=0) {mat_zeroless[j,k]<-mat[j,k]}
> }
> 	}
> ##Code End
>
> Problems arise when the algorithm encounters NAs. Numbers are passed 
> to the
> holding matrix, and zeros not, but when an NA is encountered, the 
> following
> error is generated:
>
> Error in if mat[j,k] !=0 { :missing value where TRUE/FALSE needed
>
> I'm not sure how to resolve this.
>
> Thanks,
>
> rcoder
>
>
>
>
> Henrik Bengtsson (max 7Mb) wrote:
>>
>> FYI,
>>
>> there is an isZero() in the R.utils package that allows you to 
>> specify
>> the precision.  It looks like this:
>>
>> isZero <- function (x, neps=1, eps=.Machine$double.eps, ...) {
>>   (abs(x) < neps*eps);
>> }
>>
>> /Henrik
>>
>> On Wed, Aug 13, 2008 at 8:23 AM, Roland Rau 
>> <roland.rproject at gmail.com>
>> wrote:
>>> Hi,
>>>
>>> since many suggestions are following the form of
>>> x[x==0] (or similar)
>>> I would like to ask if this is really recommended?
>>> What I have learned (the hard way) is that one should not test for
>>> equality
>>> of floating point numbers (which is the default for R's numeric 
>>> values,
>>> right?) since the binary representation of these (decimal) floating 
>>> point
>>> numbers is not necessarily exact (with the classic example of 
>>> decimal
>>> 0.1).
>>> Is it okay in this case for the value zero where all binary elements 
>>> are
>>> zero? Or does R somehow recognize that it is an integer?
>>>
>>> Just some questions out of curiosity.
>>>
>>> Thank you,
>>> Roland
>>>
>>>
>>> rcoder wrote:
>>>>
>>>> Hi everyone,
>>>>
>>>> I have a matrix that has a combination of zeros and NAs. When I 
>>>> perform
>>>> certain calculations on the matrix, the zeros generate "Inf" 
>>>> values. Is
>>>> there a way to either convert the zeros in the matrix to NAs, or 
>>>> only
>>>> perform the calculations if not zero (i.e. like using something 
>>>> similar
>>>> to
>>>> an !all(is.na() construct)?
>>>>
>>>> Thanks,
>>>>
>>>> rcoder
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
> -- 
> View this message in context: 
> http://www.nabble.com/ignoring-zeros-or-converting-to-NA-tp18948979p18970797.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list