[R] ignoring zeros or converting to NA

Charles C. Berry cberry at tajo.ucsd.edu
Wed Aug 13 05:03:30 CEST 2008


On Tue, 12 Aug 2008, stephen sefick wrote:

> 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?
>
> is.na(a) <- a==0
> make values in a NA  when a is 0?
>
> is this right?  what the logic if not?


From

 	R Language Definition
 	3.1.3 Function calls

A special type of function calls can appear on the left hand side of the 
assignment operator as in

      > class(x) <- "foo"

What this construction really does is to call the function class<- with 
the original object and the right hand side. This function performs the 
modification of the object and returns the result which is then stored 
back into the original variable. (At least conceptually, this is what 
happens. Some additional effort is made to avoid unnecessary data 
duplication.)



Also, see what

 	?Extract

says about subassignment.

Then read

 	page(`is.na<-.default`,'print')

to see an instance relevant to this thread.


Finally, you might try toying with the subassignment functions:

 	`is.na<-`(a,1)
 	`is.na<-`(a,TRUE)
 	`is.na<-`(a[1:3],TRUE)

all return the modified object (but do not actually assign it back to `a` 
as would "is.na(a) <- 1", etc.)

HTH,


Chuck



> 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
>

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



More information about the R-help mailing list