[R] Sum of logical vector

Duncan Murdoch murdoch at stats.uwo.ca
Mon Oct 31 20:48:27 CET 2005


On 10/31/2005 2:00 PM, Tuszynski, Jaroslaw W. wrote:
> Hi,
> 
> Recently I was told by users of some of the function I wrote that they
> experience crashes in places where logical vector was passed to sum
> function. However on my computer those functions work just fine. After
> closer look at documentation of function 'sum', I realized that it is
> defined only for complex and numeric vectors, so I guess I was using
> "undocumented feature". 
> 
> What I am trying to understand is why it works on some systems (my - Win XP,
> from R-1.9.? to R-2.2.0) and does not work on other (unknown platform,
> R-2.2.0):
> - Is it that they have more methods of function "sum" defined and some other
> function (maybe some package has "logical.sum" defined) is being used?
> - Different operating systems will have different behavior for supporting
> "undocumented features"?
> 
> Is there any way to automatically test your code for presence of unsupported
> functions it uses?
> Should function 'sum' produced warning/error, or convert logical to integer?
> 
> An Example code:
> 
>  repeats = function(x) return (sum(duplicated(x)) ) # how many numbers
> repeat themselves in vector x?
>  repeats( c(1:10, 3:14) )

You should always be able to sum a logical vector, because it will be 
coerced to numeric when needed.  The crashes are being caused by 
something else. Get your users to tell you exact error messages and you 
should be able to diagnose it.

Duncan Murdoch




More information about the R-help mailing list