Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-28 Thread Hervé Pagès
On 01/27/2015 02:54 AM, Bert Gunter wrote: Huh?? ifelse(TRUE, a - 2L, a - 3L) [1] 2 a [1] 2 Please clarify. In Bioconductor ifelse() is a generic function (with methods for Rle objects) so all its arguments are evaluated before dispatch can happen. You can reproduce with:

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-27 Thread JSHuang
Hi, Not sure if the following is what you look for: x - c(1:10, NA, 12:20) sum(x[!is.na(x)]) -- View this message in context: http://r.789695.n4.nabble.com/Sum-function-and-missing-values-need-to-mimic-SAS-sum-function-tp4702344p4702392.html Sent from the R help mailing list archive at

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-27 Thread Hervé Pagès
Hi Martin, On 01/26/2015 04:45 AM, Martin Maechler wrote: Jim Lemon drjimle...@gmail.com on Mon, 26 Jan 2015 11:21:03 +1100 writes: Hi Allen, How about this: sum_w_NA-function(x) ifelse(all(is.na(x)),NA,sum(x,na.rm=TRUE)) Excuse, Jim, but that's yet another horrible misuse

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-27 Thread Bert Gunter
Huh?? ifelse(TRUE, a - 2L, a - 3L) [1] 2 a [1] 2 Please clarify. -- Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 Data is not information. Information is not knowledge. And knowledge is certainly not wisdom. Clifford Stoll On Mon, Jan 26, 2015 at 2:22 PM, Hervé

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-27 Thread Sven E. Templer
Maybe this is due to the usage of rep() in ifelse(): f.rep - function(ans){ans - rep(ans,1);return(ans)} f - function(ans){return(ans)} f(a - 123) # no print here f.rep(a - 123) # prints: # [1] 123 On 27 January 2015 at 11:54, Bert Gunter gunter.ber...@gene.com wrote: Huh?? ifelse(TRUE, a -

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread Martin Maechler
Jim Lemon drjimle...@gmail.com on Mon, 26 Jan 2015 11:21:03 +1100 writes: Hi Allen, How about this: sum_w_NA-function(x) ifelse(all(is.na(x)),NA,sum(x,na.rm=TRUE)) Excuse, Jim, but that's yet another horrible misuse of ifelse() John Fox's reply *did* contain the proper

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread Sven E. Templer
you can also define 'na.rm' in sum() by 'NA state' of x (where x is your vector holding the data): sum(x, na.rm=!all(is.na(x))) On 26 January 2015 at 13:45, Martin Maechler maech...@lynne.stat.math.ethz.ch wrote: Jim Lemon drjimle...@gmail.com on Mon, 26 Jan 2015 11:21:03 +1100 writes:

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread MacQueen, Don
I'm a little puzzled by the assertion that the result is 0.0 when all the elements are NA: sum(NA) [1] NA sum(c(NA,NA)) [1] NA sum(rep(NA, 10)) [1] NA sum(as.numeric(letters[1:4])) [1] NA Warning message: NAs introduced by coercion Considering that the example snippet of code has several

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread Henrik Bengtsson
In case anyone wonders, this behavior is expected and consistent with the note the sum of an empty set is zero, by definition in help(sum), i.e. x - numeric(0) str(x) num(0) sum(x) [1] 0 Analogously, prod(numeric(0)) gives 1.0. To OP, if you're in the end of the day is after the sample

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread Allen Bingham
Message- From: Sven E. Templer [mailto:sven.temp...@gmail.com] Sent: Monday, January 26, 2015 6:56 AM To: Martin Maechler Cc: Jim Lemon; r-help mailing list; Allen Bingham Subject: Re: [R] Sum function and missing values --- need to mimic SAS sum function you can also define 'na.rm' in sum

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread Ista Zahn
Try with na.rm=TRUE. On Jan 26, 2015 4:04 PM, MacQueen, Don macque...@llnl.gov wrote: I'm a little puzzled by the assertion that the result is 0.0 when all the elements are NA: sum(NA) [1] NA sum(c(NA,NA)) [1] NA sum(rep(NA, 10)) [1] NA sum(as.numeric(letters[1:4])) [1] NA

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread Allen Bingham
. Allen -Original Message- From: MacQueen, Don [mailto:macque...@llnl.gov] Sent: Monday, January 26, 2015 1:03 PM To: Allen Bingham; r-help@r-project.org Subject: Re: [R] Sum function and missing values --- need to mimic SAS sum function I'm a little puzzled by the assertion that the result

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread Rolf Turner
On 27/01/15 13:42, Boris Steipe wrote: sum(x, na.rm=!all(is.na(x))) That's the kind of idiom that brings the poor chap who has to maintain it to tears. ;-) It looks perfectly lucid to me. If you think that that's obscure code, you ain't been around! :-) cheers, Rolf Turner --

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread Boris Steipe
sum(x, na.rm=!all(is.na(x))) That's the kind of idiom that brings the poor chap who has to maintain it to tears. ;-) __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread peter dalgaard
Ouch. Please avoid ifelse() in non-vectorized contexts. John Fox has the right idea. -pd On 26 Jan 2015, at 01:21 , Jim Lemon drjimle...@gmail.com wrote: Hi Allen, How about this: sum_w_NA-function(x) ifelse(all(is.na(x)),NA,sum(x,na.rm=TRUE)) Jim On Mon, Jan 26, 2015 at 10:21 AM,

[R] Sum function and missing values --- need to mimic SAS sum function

2015-01-26 Thread Jim Lemon
Hi, To those who pointed out my mistake, thanks. Using ifelse may not return a sensible arithmetic sum. Jim __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-25 Thread Jim Lemon
Hi Allen, How about this: sum_w_NA-function(x) ifelse(all(is.na(x)),NA,sum(x,na.rm=TRUE)) Jim On Mon, Jan 26, 2015 at 10:21 AM, Allen Bingham aebingh...@gmail.com wrote: I understand that in order to get the sum function to ignore missing values I need to supply the argument na.rm=TRUE.

[R] Sum function and missing values --- need to mimic SAS sum function

2015-01-25 Thread Allen Bingham
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,

Re: [R] Sum function and missing values --- need to mimic SAS sum function

2015-01-25 Thread John Fox
Dear Allen, This seems reasonably straightforward to me, suggesting that I might not properly understand what you want to do. How about something like the following? mysum - function(...){ + x - c(...) + if (all(is.na(x))) NA else sum(x, na.rm=TRUE) + } mysum(1, 2, 3, NA) [1] 6