Skip to content
Prev 347486 / 398498 Next

Sum function and missing values --- need to mimic SAS sum function

I understand that in order to get the sum function to ignore missing values
I need to supply the argument na.rm=TRUE. However, when summing numeric
values in which ALL components are "NA" ... the result is 0.0 ... instead of
(what I would get from SAS) of NA (or in the case of SAS ".").

Accordingly, I've had to go to 'extreme' measures to get the sum function to
result in NA if all arguments are missing (otherwise give me a sum of all
non-NA elements).

So for example here's a snippet of code that ALMOST does what I want:

 
SumValue<-apply(subset(InputDataFrame,!is.na(Variable.1)|!is.na(Variable.2),
select=c(Variable.1,Variable.2)),1,sum,na.rm=TRUE)

In reality this does NOT give me records with NA for SumValue ... but it
doesn't give me values for any records in which both Variable.1 and
Variable.2 are NA --- which is "good enough" for my purposes.

I'm guessing with a little more work I could come up with a way to adapt the
code above so that I could get it to work like SAS's sum function ... 

... but before I go that extra mile I thought I'd ask others if they know of
functions in either base R ... or in a package that will better mimic the
SAS sum function.

Any suggestions?

Thanks.
______________________________________
Allen Bingham
aebingham2 at gmail.com

Thread (17 messages)

Allen Bingham Sum function and missing values --- need to mimic SAS sum function Jan 25 John Fox Sum function and missing values --- need to mimic SAS sum function Jan 25 Jim Lemon Sum function and missing values --- need to mimic SAS sum function Jan 25 Peter Dalgaard Sum function and missing values --- need to mimic SAS sum function Jan 26 Martin Maechler Sum function and missing values --- need to mimic SAS sum function Jan 26 Sven E. Templer Sum function and missing values --- need to mimic SAS sum function Jan 26 MacQueen, Don Sum function and missing values --- need to mimic SAS sum function Jan 26 Ista Zahn Sum function and missing values --- need to mimic SAS sum function Jan 26 Henrik Bengtsson Sum function and missing values --- need to mimic SAS sum function Jan 26 Allen Bingham Sum function and missing values --- need to mimic SAS sum function Jan 26 Allen Bingham Sum function and missing values --- need to mimic SAS sum function Jan 26 Hervé Pagès Sum function and missing values --- need to mimic SAS sum function Jan 26 Boris Steipe Sum function and missing values --- need to mimic SAS sum function Jan 26 Rolf Turner Sum function and missing values --- need to mimic SAS sum function Jan 26 Bert Gunter Sum function and missing values --- need to mimic SAS sum function Jan 27 Sven E. Templer Sum function and missing values --- need to mimic SAS sum function Jan 27 Hervé Pagès Sum function and missing values --- need to mimic SAS sum function Jan 28