treat NA's as zero when summed up with numbers

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

treat NA's as zero when summed up with numbers

Samir Benzerfa
Hello everybody,

 

is there any way to treat NA's as zero when they are summed up with numbers,
but to treat them as NA's when summed up only with NA's. Specifically want
that: 5+NA=5, but NA+NA=NA (and not zero).

 

Any ideas?

 

Best, S.B.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: treat NA's as zero when summed up with numbers

David Winsemius

On Oct 12, 2011, at 12:45 PM, Samir Benzerfa wrote:

> Hello everybody,
>
>
>
> is there any way to treat NA's as zero when they are summed up with  
> numbers,
> but to treat them as NA's when summed up only with NA's.  
> Specifically want
> that: 5+NA=5, but NA+NA=NA (and not zero).
>

sum(x , na.rm=TRUE)

--
David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: treat NA's as zero when summed up with numbers

David Winsemius

On Oct 12, 2011, at 1:33 PM, David Winsemius wrote:

>
> On Oct 12, 2011, at 12:45 PM, Samir Benzerfa wrote:
>
>> Hello everybody,
>>
>>
>>
>> is there any way to treat NA's as zero when they are summed up with  
>> numbers,
>> but to treat them as NA's when summed up only with NA's.  
>> Specifically want
>> that: 5+NA=5, but NA+NA=NA (and not zero).
>>
>
> sum(x , na.rm=TRUE)

When I read that a second time I realized my reply was unresponsive.

 > if (all( is.na(NA) )) NA else sum( NA, na.rm=TRUE)
[1] NA
 > if (all( is.na(c(1,NA)) )) NA else sum( c(1,NA), na.rm=TRUE)
[1] 1


>
> --
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: treat NA's as zero when summed up with numbers

Bert Gunter
How about, more simply:

> mysum <- function(x)sum(x,na.rm = any(!is.na(x)))
> mysum(c(1,NA))
[1] 1
> mysum(c(NA,NA))
[1] NA

-- Bert

On Wed, Oct 12, 2011 at 11:03 AM, David Winsemius <[hidden email]>wrote:

>
> On Oct 12, 2011, at 1:33 PM, David Winsemius wrote:
>
>
>> On Oct 12, 2011, at 12:45 PM, Samir Benzerfa wrote:
>>
>>  Hello everybody,
>>>
>>>
>>>
>>> is there any way to treat NA's as zero when they are summed up with
>>> numbers,
>>> but to treat them as NA's when summed up only with NA's. Specifically
>>> want
>>> that: 5+NA=5, but NA+NA=NA (and not zero).
>>>
>>>
>> sum(x , na.rm=TRUE)
>>
>
> When I read that a second time I realized my reply was unresponsive.
>
> > if (all( is.na(NA) )) NA else sum( NA, na.rm=TRUE)
> [1] NA
> > if (all( is.na(c(1,NA)) )) NA else sum( c(1,NA), na.rm=TRUE)
> [1] 1
>
>
>
>> --
>> David Winsemius, MD
>> West Hartford, CT
>>
>> ______________________________**________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________**________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide http://www.R-project.org/**
> posting-guide.html <http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>



--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Loading...