[R] ignoring zeros or converting to NA
Steven McKinney
smckinney at bccrc.ca
Wed Aug 13 04:33:31 CEST 2008
The help page for is.na()
is worth reading repeatedly.
> -----Original Message-----
> From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org]
> On Behalf Of stephen sefick
> Sent: Tuesday, August 12, 2008 6:53 PM
> To: Charles C. Berry
> Cc: Mike Prager; r-help at stat.math.ethz.ch
> Subject: Re: [R] ignoring zeros or converting to NA
>
> I have been reading this thread and I am having a hard interpreting
> what these mean. I know that the result is that all of the values
> that are zero in a are replaced by NA. Let me try and write it out
>
> is.na(a[a==0] ) <- TRUE
> you pull out of a all of the times that are equal to zero then is.na
> tests and returns false then all of the false values are set to true?
Not quite - the function 'is.na<-' needs an index vector
as input. So my understanding is that all the values in 'a'
that are zero will have their value set to NA, because
R will replicate the TRUE (vector of length 1) to
a vector of length (sum(a==0)), this being the index vector.
>
> is.na(a) <- a==0
> make values in a NA when a is 0?
Right. a==0 generates a logical vector with length
(length(a)) with value 'TRUE' wherever 'a' has a
zero entry. 'is.na<-' will then set those entries to NA.
The entries where 'a==0' is FALSE will not have their
values changed to NA.
The version ' is.na(a[a==0] ) <- TRUE '
is doing the same thing, the logical index vector
for ' a[a==0] ' consists of all 'TRUE'.
HTH
Steve McKinney
>
> is this right? what the logic if not?
> thanks
>
> Stephen Sefick
>
>
> On Tue, Aug 12, 2008 at 6:32 PM, Charles C. Berry
<cberry at tajo.ucsd.edu>
> wrote:
> > 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] ) <- TRUE
> >
> > Or
> > 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
> >
> > ______________________________________________
> > 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
>
> ______________________________________________
> 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