[Rd] bug or feature? (PR#4150)
Duncan Murdoch
dmurdoch at pair.com
Sat Sep 13 09:42:13 MEST 2003
On Sat, 13 Sep 2003 13:37:28 +0200 (MET DST), you wrote:
>Full_Name: Axel Benz
>Version: 1.7.1
>OS: Windows
>Submission from: (NULL) (137.251.33.43)
>
>
>This feature seems to be a basic bug:
>
>> 1=="1"
>[1] TRUE
>> as.numeric(1)=="1"
>[1] TRUE
>> as.numeric(1)==as.character("1")
>[1] TRUE
>
>isn't it necessary to distinguish beteen numbers and characters??
No, the general rule in R is that operators try to coerce their
arguments into a suitable type for the operation. In this case,
because one arg is character, the other one is coerced to character
too. It doesn't matter that you forced the type using as.numeric or
as.character; the operator is going to force them both to comparable
types.
For example,
> as.character(pi)
[1] "3.14159265358979"
> pi == "3.14159265358979"
[1] TRUE
> pi == 3.14159265358979
[1] FALSE
In the last case, they were already comparable, so no forcing was
done. Since the internal value of pi actually carries more than 14
decimal places of precision, they don't compare equal as numbers, even
though the do compare equal when converted to character.
If you want to distinguish numbers and characters, use mode(1) ==
mode("1").
Duncan Murdoch
More information about the R-devel
mailing list