[R] treat NA's as zero when summed up with numbers

David Winsemius dwinsemius at comcast.net
Wed Oct 12 20:03:57 CEST 2011


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

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list