[R] Error:non-numeric argument in my function

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Jun 1 17:33:56 CEST 2009


i think the error message might be even better, but this would require
'* to be even better.  i know some will take it for lamenting:  there is
an ugly lack of consistency here:

    1:2
    # 1 2
    1:2.5
    # 1 2 (coercion double -> integer)
    1:'2'
    # 1 2 (corecion character -> integer)
    1:'a'
    # Error in 1:"a" : NA/NaN argument
    # In addition: Warning message:
    # NAs introduced by coercion

    1 * 2
    # 2
    1 * '2'
    # Error in 1 * "2" : non-numeric argument to binary operator
    1 * '2'
    # Error in 1 * "a" : non-numeric argument to binary operator

in the two last cases, i'd expect, respectively, a successful
computation (with a character -> numeric coercion of '2' to 2), and an
error similar to the one for 1:'a'. 

the current error message isn't good, and what pat and stavros propose
seems much better;  i think it could be even better if 1 * '2' worked,
and if 1 * 'a' caused an error of the form "cannot coerce 'a' to numeric
for use with the arithmetic operator '*'".

vQ



Stavros Macrakis wrote:
> Agreed, that's even better, e.g.
>
>     Error in 1 * "a" : character argument not allowed for arithmetic
> operator *
>
> For some reason (does anyone know the rationale?), in the case of factors,
> you don't get an error, but a more explicit warning and an NA result:
>
>   
>> 2*factor(3)
>>     
> [1] NA
> Warning message:
> In Ops.factor(2, factor(3)) : * not meaningful for factors
>
> This seems hazardous, especially since the user has to be sophisticated
> enough to know about options(warn=2) to get a traceback for this.
>
> As for data frames, arithmetic operators seem to work if all the values are
> numeric:
>
>   
>> 2*data.frame(a=1)
>>     
>   a
> 1 2
>
> It's a hard problem to make useful error messages for beginning users....
>
>             -s
>
>
> On Mon, Jun 1, 2009 at 4:34 AM, Patrick Burns <pburns at pburns.seanet.com>wrote:
>
>   
>> I thought Stavros' suggestion was going
>> to be to have the error message say what
>> type of offending object was found.  If
>> the message said that a list of class
>> 'data.frame' was found (probably the leading
>> case), then that would be much more helpful.
>>
>> Patrick Burns
>> patrick at burns-stat.com
>> +44 (0)20 8525 0696
>> http://www.burns-stat.com
>> (home of "The R Inferno" and "A Guide for the Unwilling S User")
>>
>> Stavros Macrakis wrote:
>>
>>     
>>> On Sun, May 31, 2009 at 6:10 PM, jim holtman <jholtman at gmail.com> wrote:
>>>
>>>  Message is very clear:
>>>       
>>>>  1 * 'a'
>>>>         
>>>> Error in 1 * "a" : non-numeric argument to binary operator
>>>>
>>>>         
>>> Though the user should have been able to figure this out, perhaps the
>>> error
>>> message could be improved? After all, it is not the fact that the operator
>>> is *binary* that implies that its argument must be numeric, but that it is
>>> *arithmetic*. The binary operator %in%, for example, takes non-numeric
>>> arguments.
>>>
>>> Suggested replacement error message:
>>>
>>>     non-numeric argument to arithmetic operator
>>>
>>>               -s




More information about the R-help mailing list