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 -- View this message in context: http://www.nabble.com/ignoring-zeros-or-converting-to-NA-tp18948979p18948979.html Sent from the R help mailing list archive at Nabble.com.
when you read it in na.string=0 On Tue, Aug 12, 2008 at 1:43 PM, rcoder <mpdotbook at gmail.com> 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 > -- > View this message in context: http://www.nabble.com/ignoring-zeros-or-converting-to-NA-tp18948979p18948979.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. >-- Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
rcoder <mpdotbook at gmail.com> wrote:> 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)?Is this what you are looking for?> # make some data > a = matrix(c(rep(0,6), rep(2,6)), nrow = 4) > a[,1] [,2] [,3] [1,] 0 0 2 [2,] 0 0 2 [3,] 0 2 2 [4,] 0 2 2> # change zero to NA > is.na(a[a==0] ) <- TRUE > a[,1] [,2] [,3] [1,] NA NA 2 [2,] NA NA 2 [3,] NA 2 2 [4,] NA 2 2 -- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement.
On Tue, 12 Aug 2008, Mike Prager wrote:> rcoder <mpdotbook at gmail.com> wrote: > >> 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)? > > Is this what you are looking for? > >> # make some data >> a = matrix(c(rep(0,6), rep(2,6)), nrow = 4) >> a > [,1] [,2] [,3] > [1,] 0 0 2 > [2,] 0 0 2 > [3,] 0 2 2 > [4,] 0 2 2 >> # change zero to NA >> is.na(a[a==0] ) <- TRUEOr is.na(a) <- a==0 Chuck>> a > [,1] [,2] [,3] > [1,] NA NA 2 > [2,] NA NA 2 > [3,] NA 2 2 > [4,] NA 2 2 > > -- > Mike Prager, NOAA, Beaufort, NC > * Opinions expressed are personal and not represented otherwise. > * Any use of tradenames does not constitute a NOAA endorsement. > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
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
The help page on binary operators (see ?"==") confirms that binary representation of fractional representation is not catered for and points to all.equal as a more suitable test method for those cases. Steve E>>> Thomas Lumley <tlumley at u.washington.edu> 13/08/2008 16:47 >>>Integers (up to a fairly high limit) are represented exactly, as are fractions whose denominator is a power of two (again up to a fairly high limit), so x==0 is fine in that sense. If x is computed by floating point operations you do have to worry whether these are exact, eg, with x<-seq(-1,1,length=7) it is not clear that the fourth element will be exactly zero. -thomas On Wed, 13 Aug 2008, Roland Rau 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 forequality of> floating point numbers (which is the default for R's numeric values,right?)> since the binary representation of these (decimal) floating pointnumbers 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 elementsare 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 Iperform>> certain calculations on the matrix, the zeros generate "Inf" values.Is>> there a way to either convert the zeros in the matrix to NAs, oronly>> perform the calculations if not zero (i.e. like using somethingsimilar 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. >Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle ______________________________________________ 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. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
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.
Sorry for jumping in. I haven't read the entire conversation. But imagine you want to compute 1/x for the entire matrix and your matrix has 0s and NAs. Then you could do: ##define function f<-function(x){1/x} ##sample data y=c(0,1,2,3,4,5,6,7,NA) ##arrange in matrix mat=matrix(y,3,3) ##apply function to matrix "mat" ##except 0s and NAs calc.mat=ifelse(mat==0|is.na(mat)==T,NA,f(mat)) ##inspect resulting matrix calc.mat If you are concerned about near zeros instead of zeros, you could adjust the ifelse condition. Best, Daniel 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 >-- View this message in context: http://www.nabble.com/ignoring-zeros-or-converting-to-NA-tp18948979p18989410.html Sent from the R help mailing list archive at Nabble.com.