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 <dwinsemius@comcast.net>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
>>
>> ______________________________**________________
>> R-help@r-project.org 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
>
> ______________________________**________________
> R-help@r-project.org 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]]

